Image is mainly opened after the picture, the image is edited, mainly has the following common functions:
1. Read and display the picture:
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ') Img.show ()
2. Display Image size:
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ') print (img.size)
3. Rotate the picture 40 degrees and display:
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ') Img.rotate.show ()
4. Display image name (including path):
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ') print (img.filename)
5, the picture shrinks one times (probably so the algorithm):
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ') Img.thumbnail ((IMG.SIZE[0]/2,IMG.SIZE[1]/2))
6, the pixel scale of the picture:
From PIL Import imageimg = Image.open ("H:\\salary.png", ' r ') img_new = Image.eval (Img,lambda x:x*2)
Img_new.show ()
7, image synthesis or fusion:
From PIL import Image
#img1和img2大小要一样, and one is transparent, otherwise you can only see a picture PIL.image.alpha_composite (IMG1,IMG2)
#alpha表示透明度
PIL.image.blend (Im1,im2,alpha)
PIL.Image.composite (Im1,im2,mask)
8. Create and save the image:
From PIL import Image
#使用RGB模式创建一个黑色的图形 (also L and RGBA mode)
img = image.new (' RGB ', (x, y), (0,0,0))
#L模式创建图形
img = image.new (' L ', (x, y), 255)
#RGBA模式创建图形
img = image.new (' RGBA ', (x, y), (0,0,0,255))
Img.show ()
Img.save (' Img.png ')
#查看模式
Print (Img.mode)
9, the image of the Black (gray):
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ') Img.convert ("L"). Show ()
10, the image copy:
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ')
#这里也可以直接赋值
#img1 = IMGIMG1 = Img.copy ()
Img1.show ()
11. Obtain the corresponding coordinates of the pixel:
From PIL Import imageimg = Image.open ("H:\\salary.png", ' R ')
Print (Img.getpixel (x, y))
12. Paste an image onto another image:
From PIL import Image
#俩个图片的mode要一致, inconsistencies can be converted with convert IMG1 = Image.open ("H:\\salary.png", ' R ')
Img2 = Image.open ("H:\\pig.png", ' R ')
Img1.paste (Img2, (50,15))
Img1.show ()
13. Draw a line on the given picture:
From PIL import Image
From PIL Import imagedrawimg = Image.open ("H:\\salary.png", ' R ')
Draw = Imagedraw.draw (img)
#传入俩个元祖参数, the first one is the two endpoint coordinates of the line, the other is the RGB
Draw.line ((22,43,99,11), (150,150,150))
Img.show ()
14. Write text on a given image:
From PIL import Image
From PIL import Imagedraw
From PIL Import imagefontimg = Image.open ("H:\\salary.png", ' R ')
Draw = Imagedraw.draw (img)
Font = Imagefont.truetype (' Segoeuib.ttf ',20)
#传入4个参数: Text start coordinates, text, colors, and fonts (fonts can not be passed in, using default fonts)
Draw.text ((50,60), ' Hello,world ', (140,255,166), font = font)
Img1.show ()
Python learning PIL module basic functions