Reptiles (XIII): the PIL module crawler (XII): identification pattern verification code, the identification code verification slider (B slidable station codes)

1. PIL module

In reptiles (XII): identification code of the graphic, slide verify the identification code (B station sliding verification code) , I left a suspense, why are installing a pillow block, rather than the PIL module. This is because PIL is the product of python2, it does not follow the development of python and development. For this reason it is big brother wrote a pillow block for python3 deliberately. So, if you need to install python3 corresponding PIL, should choose to install pillow.

1.1 Image Import module

We generally use the PIL module Image module, so I'll just explain the Image module.

Installing PIL module:

pip install pillow -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

Import module:

from PIL import Image

1.2 Image Module conventional method

(1) read the image

open (url): reading a picture, parameter is the name of the picture.

(2) show pictures

show (): display a picture.

Case:

from PIL import Image

img = Image.open('xhh.jpg')
img.show()

result:

(3) to save the image

save (image, type): Save the image as the name for the "image", the format for the type of picture format.

(4) Create a new image

new (mode, size): create a model for the mode, the picture size to size.

new (mode, size, color): create a model for the mode, size size, color color pictures.

Case:

from PIL import Image

newImg = Image.new("RGBA",(640,480),(255,0,0))
newImg.save("newImg.png","PNG")

result:

(5) adding the two pictures

blend (img1, img2, alpha): The img1 and img2, where alpha represents the proportion of parameters img1 and img2 

(6) changing the image size

resize (size): modify the image size SIZE

Case 1:

from PIL import Image

img1 = Image.open("xhh.jpg")
img2 = Image.open("xhh2.jpg")
#img = img1×0.8+img2×0.2
img = Image.blend(img1,img2,0.2)
img.show()

result:

Why is it being given? Because their sample sizes, and this is not being given an official explanation.

Case 2:

from PIL import Image

img1 = Image.open("xhh.jpg")
img1 = img1.resize((256,256))
img2 = Image.open("xhh2.jpg")
img2 = img2.resize((256,256))
#img = img1×0.7+img2×0.3
img = Image.blend(img1,img2,0.3) img.show()

结果:

(7) 点操作 

point(function):这个function接受一个参数,且对图片中的每一个点执行这个函数。

案例:

from PIL import Image

img = Image.open("xhh.jpg")
img.show()
out=img.point(lambda i:i*1.5)#对每个点进行50%的加强
out.show()

这前后对比明显吧。 

(8) 查看图像信息

from PIL import Image

img = Image.open("xhh.jpg")
print(img.format)
print(img.size)
print(img.mode)

结果:

(9) 图片裁剪

crop(box):设置要裁剪的区域范围box。

案例:

from PIL import Image

img = Image.open("xhh.jpg")
box=(100,100,500,500)
#设置要裁剪的区域
region=img.crop(box) #此时,region是一个新的图像对象。
region.show()

结果:

(10) 图像黏贴(合并)

paste(region,box):黏贴box大小的region图像到原来的图片对象中。

案例:

from PIL import Image

img1 = Image.open("xhh.jpg")
img2 = Image.open("xhh2.jpg")
img1.paste(img2,(0,0))
img1.show()

结果:

(11) 通道分离

split():分割成三个通道,此时r,g,b分别为三个图像对象。

(12) 通道合并

merge("RGB",(b,g,r)):将b,r两个通道进行翻转。

案例:

from PIL import Image

img1 = Image.open("xhh.jpg")
img1.show()
r, g, b = img1.split()
img2 = Image.merge("RGB", (b, g, r))
img2.show()

结果:

(13) 旋转图像

rotate(angle):逆时针旋转angle度。 

(14) 图像转换

案例:

from PIL import Image

img = Image.open("xhh.jpg")
#左右对换
out1 = img.transpose(Image.FLIP_LEFT_RIGHT)
out1.show()
#上下对换
out2 = img.transpose(Image.FLIP_TOP_BOTTOM)
out2.show()

结果:

(15) 图像类型转换

convert(mode):将图像转换成mode类型。

PIL的九种不同模式:1,L,P,RGB,RGBA,CMYK,YCbCr,I,F。

常用的两种:

1.模式”1”
为二值图像,非黑即白。但是它每个像素用8个bit表示,0表示黑,255表示白。下面我们将lena图像转换为“1”图像。
from PIL importImage
img = Image.open(“E:\image\myimg.jpg”)
img_1=img.convert(“1”)
2.模式“L”
为灰色图像,它的每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。在PIL中,从模式“RGB”转换为“L”模式是按照下面的公式转换的:
L = R * 299/1000 + G * 587/1000+ B * 114/1000

(16) 获取某个像素位置的值

getpixel(coordinate):coordinate为(x,y)形式的值。

(17) 修改某个像素位置的值

putpixel(coordinate,color):修改像素位置coordinate的颜色color的值。

案例:

from PIL import Image

img = Image.open("xhh.jpg")

print(img.size)
print(img.getpixel((4, 4)))

width = img.size[0]  # 长度
height = img.size[1]  # 宽度

for w in range(0, width):
    for h in range(0, height): data = img.getpixel((w, h)) # 得到像素值 if (data[0] <= 170 and data[1] <= 170 and data[2] <= 170): img.putpixel((w, h), (0, 0, 255)) # 则这些像素点的颜色改成大红色  img.show()

结果:

Guess you like

Origin www.cnblogs.com/liuhui0308/p/12099467.html