Python Pillow库学习

一.安装pillow

很简单直接:pip install pillow
导入的时候python2和python3略有不同
//python2
import Image
//python3(因为是派生的PIL库,所以要导入PIL中的Image)
from PIL import Image

二.基本操作

1.图片的打开和展示

1
2
3
from PIL import Image
im=Image.open("haha.jpg")
im.show()

open()打开图片
show()展示图片

2.图片格式,宽高,模式

1
2
3
from PIL import Image
im=Image.open("haha.jpg")
print(im.format,im.size,im.mode)

format展示图像的格式(jpg,png等等)
size属性是一个tuple,表示图像的宽和高
mode表示图像的模式(RGB)

3.图片保存

1
2
3
from PIL import Image
im=Image.open("haha.jpg")
im.save("/root/haha1.jpg")

save()保存图片

4.convert()

convert() 是图像实例对象的一个方法,接受一个 mode 参数,用以指定一种色彩模式,mode 的取值可以是如下几种:
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· L (8-bit pixels, black and white)
· P (8-bit pixels, mapped to any other mode using a colour palette)
· RGB (3x8-bit pixels, true colour)
· RGBA (4x8-bit pixels, true colour with transparency mask)
· CMYK (4x8-bit pixels, colour separation)
· YCbCr (3x8-bit pixels, colour video format)
· I (32-bit signed integer pixels)
· F (32-bit floating point pixels)

1
2
3
from PIL import Image
im=Image.open("haha.jpg").convert('L')
im.show()

1
2
3
from PIL import Image
im=Image.open("haha.jpg").convert('RGB")
im.show()

5.filter()

用filter()需要导入PIL库中的ImageFilter模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

from PIL import Image, ImageFilter
im = Image.open(‘1.png')
# 高斯模糊
im.filter(ImageFilter.GaussianBlur)
# 普通模糊
im.filter(ImageFilter.BLUR)
# 边缘增强
im.filter(ImageFilter.EDGE_ENHANCE)
# 找到边缘
im.filter(ImageFilter.FIND_EDGES)
# 浮雕
im.filter(ImageFilter.EMBOSS)
# 轮廓
im.filter(ImageFilter.CONTOUR)
# 锐化
im.filter(ImageFilter.SHARPEN)
# 平滑
im.filter(ImageFilter.SMOOTH)
# 细节
im.filter(ImageFilter.DETAIL)

6.获取图片尺寸,缩放图片

1
2
3
4
5
6
7
from PIL import Image
im=Image.open("haha.jpg")
w,h=im.size
print(w,h)
im.thumbnail((w//2,h//2))
#im=im.resize((217,154))
print("Resize image to:%s %s"%(w//2,h//2))

7.截屏

1
2
3
from PIL import ImageGrab
im=ImageGrab.grab((0,0,800,200))#截取屏幕指定区域的图像
im=ImageGrab.grab()#全屏截屏

8.羽化

1
2
3
4
from PIL import Image,ImageFilter
im=Image.open("haha.jpg")
im2=im.filter(ImageFilter.BLUR)
im2.save('1.jpg','jpeg)

9.颜色与RGBA值

传统的图片模式是RGB 即红绿蓝 ;RGBA是红绿蓝加上alpha(透明度)。RGBA的值表示为由4个整数组成的元组,分别R,G,B,A整数的范围0~255,RGB全0表示黑色,全255表示黑色。那么猜测(0,128,0,255)是绿色,因为G分量最大,R、B分量都是0,所以呈现出来是绿色。但是当alpha值为0时,无论什么颜色,该颜色都是不可见的。

1
2
3
from PIL import ImageColor
print(ImageColor.getcolor('green','RGBA'))
print(ImageColor.getcolor('black','RGB'))

10.图像的坐标表示

图像中左上角是坐标原点(0,0),这和平常数学里的坐标系不太一样。这样定义的坐标意味着,X轴是从左到右增长的,而Y轴是从上到下增长。在Pillow中如何使用上述定义的坐标系表示一块矩形区域?许多函数或方法要求提供一个矩形元组参数。元组参数包含四个值,分别代表矩形四条边的距离X轴或者Y轴的距离。顺序是(左,顶,右,底)。右和底坐标稍微特殊,表示直到但不包括。比如(3,2,8,9)就表示横坐标[3,7];纵坐标[2,8]的矩形区域。

11.新建图像(new())

new()有三个参数 第一个参数是mode即颜色空间模式,第二个参数指定了图像的分辨率(宽×高),第三个参数是颜色(可以省略)
第三个参数颜色:可以直接填入常用颜色名称(red,green,blue);也可以填入十六进制表示的颜色,如#FF0000表示红色;还能传入元组,如(255,0,0,255)表示红色

12.更改单个像素点(putpixel())

getpixel() 获取单个像素点的RGB或者RGBA值
putpixel()在单个像素点上添加RGB或者RGBA值

1
2
3
4
5
6
7
8
9
from PIL import ImageColor,Image
im=Image.new('RGB',(200,200))
print(im.getpixel((0,0)))
for i in range(200):
for j in range(100):
im.putpixel((i,j),(210,210,210))
for z in range(100,200):
im.putpixel((i,z),(255,0,0))
im.save("4.png")

13.图片的剪贴,黏贴

(1)图片的剪贴
1
2
3
4
5
from PIL import Image
im=Image.open("haha.jpg")
box=(100,100,450,300)
region=im.crop(box)
region.show()

box这是一个4元的坐标数组,坐标轴是左上角是(0,0)的卡迪尔坐标系。box(x1,y1,x2,y2)

(2)图片的黏贴

paste() 图片黏贴方法
paste(要贴的图片,要贴的图片的4元坐标组成的区域)

1
2
3
4
5
6
7
from PIL import Image
im=Image.open("haha.jpg")
box=(50,50,200,200)
region=im.crop(box)
#region=region.transpose(Image.ROTATE_180)
im.paste(region,box)
im.show()

14.调整图像大小

resize((x,y)) 将图片改成宽为x,高为y的图片

1
2
3
4
5
6
from PIL import Image
im=Image.open('haha.jpg')
width,height=im.size
#print(width,height)
resizeim=im.resize((width,height+10))
resizeim.show()

15.旋转和翻转图像

1
2
3
4
5
from PIL import Image
im=Image.open('haha.jpg')
im.rotate(90).show()
im.rotate(270).show()
im.rotate(180).show()

处理GIF等序列文件

使用seek和tell方法可以在不同帧移动,tell是帧数,而seek是取当前帧数的图片。

1
2
3
4
5
from PIL import Image
im=Image.open('1.gif')
while 1:
im.seek(im.tell()+1)
im.show()

三.常用脚本

1.LSB

LSB最低位隐写 就是把隐藏的信息放到每个像素的最低位

LSB是将原本的像素转8位2进制,将8位2进制的左后一位置0或者置1来隐写数据,所以我们可以枚举所有像素,当该位像素最后一位不为0时,置为255的黑点。

1
2
3
4
5
6
7
8
9
10
11
from PIL import Image
img=Image.open('01.png')
width,height=img.size
for i in range(0,width):
for j in range(0,height):
tmp=img.getpixel((i,j))
if tmp&0x1==0:
img.putpixel((i,j),0)
else:
img.putpixel((i,j),255)
img.show()

2.RGB画图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from PIL import Image

x = 280 #x坐标 通过对txt里的行数进行整数分解
y = 280 #y坐标 x * y = 行数

im = Image.new("RGB", (x, y)) #创建图片
file = open('flag.txt') #打开rbg值的文件

#通过每个rgb点生成图片

for i in range(0, x):
for j in range(0, y):
line = file.readline() #获取一行的rgb值
rgb = line.split(", ") #分离rgb,文本中逗号后面有空格
im.putpixel((i, j), (int(rgb[0]), int(rgb[1]), int(rgb[2]))) #将rgb转化为像素

im.show()

3.图片去污垢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from PIL import Image
lena = Image.open('1.png')
pixsels = lena.load()
width=lena.size[0]
height=lena.size[1]
list1=[]
for x in range(0,width):
for y in range(0,height):
r,g,b=pixsels[x,y]
if r==255 and g==255:
pass
else:
if int(bin(b)[-1])==1:
list1.append(0)
else:
list1.append(1)
print (len(list1))
im=Image.new("1",(300,300))
i=0
while i<len(list1):
im.putpixel((i%300,i/300),list1[i])
i=i+1
im.save("2.png")

4.方向建画图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PIL import Image

MAX=1000

pic=Image.new("RGB",(MAX,MAX))

str="DDDDDDDDDRRRRRRDDDDDDDDDDDDDDDDLLLDDDDDDDDDDDLLRRRRLLDDDDDDDDDDDDDDDDDDDDDDDDUUUUUUUUUUUUUUUUUUUURRRRRRRUUUUUUUUUUUUUUUDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDUUUUUUUUUUUUUUUUUUUURRRRRRRRUUUUUUUUUUULLLLLRRRRRRLDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLUUUUUUUUUUUUUURRRRUUUUURRRRRUUUUUUUUUUURRRRRDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLUUUUUUUUUUUUUUUUUUUDDDDDDDDDDDDDDDDDDDRRRRRRLDDDDDDDDDDDDDLLLLLLLRRRRRRRRLUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUURRRRRUUUUUUUUUUUUUUUUURRLLDDDDDDDDDDDDDDDDDDDDDDLLDDDDRRDDDDDDDDDDDDDDDDDDDDDDDRRLLUUUUUUUUUUUUUUUUUUUUUUULLUUUURRUUUUURRRRRRRRUUUUUUUUUUULLLLLRRRRRRLDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLUUUUUUUUUUUUUURRRRUUUUURRRRDDDDDDDDDDDDDDDDDDDDDDRRRRRLLLLLUUUUUUUUUUUUUUUUUUUUUURRRRRUUUUUUUUUUUUUUUULLLLLRRRRRDDDDDDDDDDDDDDDDRRRUUUUUUUUUUUUUUUURRRRLLLLDDDDDDDDDDDDDDDDRRRRDDDDDDDDDDDDDDDDDDDDDDLLLLRRRRUUUUUUUUUUUUUUUUUUUUUURRRRRUUUUUUUUUUUUUUUURRRRRDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLUUUUUUUUUUUUUUUUUUUUUURRRRRRRRRRRRUUUUUUUUUUUUUUUULLLLLRRRRRDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLRRRRRUUUUUUUUUUUUUUUUUUUUUURRRRRRRUUUUUUUUUUUUUUUULLLLLRRRRRDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLRRRRRUUUUUUUUUUUUUUUUUUUUUURRUUUUUUUUUUUUUUUURRRRRDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLUUUUUUUUUUUUUUUUUUUUUURRRRRRRRRRUUUUUUUUUUUUUUUUUUUUULLLRRRDDDDDDDDDDDDDDDDDDDDDDDDRRDDDDLLDDDDDDDDDDDDDDDDDDDDLLL"

flagx=10
flagy=50
for y in range(0,MAX):

for x in range(0,MAX):
pic.putpixel([x,y],(255,255,255))
for key in str:
if key=="R":
for x in range(0,5):
pic.putpixel([flagx+x,flagy],(0,0,0))
flagx=flagx+5
if key=="D":
for x in range(0,5):
pic.putpixel([flagx,flagy+x],(0,0,0))
flagy+=5
if key=="L":
for x in range(0,5):
pic.putpixel([flagx-x,flagy],(0,0,0))
flagx-=5
if key=="U":
for x in range(0,5):
pic.putpixel([flagx,flagy-x],(0,0,0))
flagy-=5
pic.show()

pic.save("flag.png")

当然常用脚本不只这些 python对图片处理有着强大的功能,还需不断学习。

第二篇文章了,不知道有没有人看,希望自己坚持下去!!

#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×