PIL: is the abbreviation of the Python Image Library, the module of the graphics processing. Image,imagefont,imagedraw,imagefilter
Image module:
Common methods:
Open ()#Open PictureNew (Mode,size,color)#Create a new pictureSave ("Test.gif","GIF")#Save (new picture path and name, save format)size ()#Get picture sizethumbnail (weight,high)#Zoom picture size (wide, high)Show ()#Show PicturesBlend (Img1,img2,alpha)#two pictures added, Alpha represents the proportional parameters of img1 and Img2. crop ()#extracts an image of a matrix size. It receives a tuple of four elements as a parameter, each element being (left, upper, right, lower), and the origin of the coordinate system (0, 0) is the upper-top corner. Rotate (45)#rotate counterclockwise 45 degreestranspose ()#Rotate ImageTranspose (Image.flip_left_right)#swap right and left. Transpose (Image.flip_top_bottom)#swap up and down. Transpose (IMAGE.ROTATE_90)#rotates at a 90-degree angle. Transpose (image.rotate_180)#rotates at a 180-degree angle. Transpose (image.rotate_270)#rotates at a 270-degree angle. paste (Im,box)#Paste the box-size IM into the original picture object. convert ()#used to convert an image to a different color mode. Filters ()#FilterBLUR#BlurEMBOSSResize ((128,128))#resize into 128*128 pixel sizeCONVERT ("RGBA")#Graphics type ConversionGetPixel ((bis))#gets the value of a pixel positionPutpixel ((bis), (255,0,0))#the value to write to a pixel location
Instance:
ImportImage,imagefilterim= Image.open ("e:\\qq2.jpg")#Open PictureW,h = Im.size#gets the pixel of the picture, and its value is the width and height of the pixel meaningPrint(Im.format)#Get Picture FormatPrint(im.size)#Get picture pixelsIm.thumbnail ((W//2,H//2))#scale the image to half the size of the originalIm.save ("e:\\qq_backup.jpg",'JPEG')#Save the picture, and save the format as JPEG#im.show () #显示图片Print(Im.mode)#display mode. RGB (True color image), plus, L (luminance), CMTK (pre-press image). box = (100,100,200,200) s= Im.crop (Box)#an image of a rectangular size is extracted from the image. It receives a tuple of four elements as a parameter, each element being (left, upper, right, lower), and the origin of the coordinate system (0, 0) is the upper-top corner. #s.show ()#s = s.transpose (image.rotate_180) #旋转180 °#im.paste (s,box) #粘贴#im.show ()im= Image.open ("e:\\qq2.jpg") im2=Im.filter (Imagefilter.emboss) Im2.save ("e:\\qq_2.jpg",'JPEG')#im2.show ()newimg= Image.new ("RGBA", (640,480), (0,255,0))#Create a picture with a size of 640*480 color greenNewimg.save ("E:\\newimg.png","PNG")
The output is:
JPEG (640, 640) RGB
Imagefont module:
Common methods:
Imagedraw module:
Example: Generating a verification code
#!/usr/bin/env python#-*-coding:utf-8-*-ImportImage, Imagedraw, Imagefont, ImageFilterImportRandom#Random Letters:defRndchar ():returnChr (Random.randint (65, 90))#random color 1:defRndcolor ():return(Random.randint (255), Random.randint (255), Random.randint (64, 255))#Random color 2:defRndColor2 ():return(Random.randint (127), Random.randint (127), Random.randint (32, 127))#x:width = 60 * 4Height= 60Image= Image.new ('RGB', (width, height), (255, 255, 255))#To Create a Font object:Font = Imagefont.truetype ("C:\\windows\\fonts\\arial.ttf", 36)#note the path to the system font#To create a draw object:Draw =Imagedraw.draw (image)#fill each pixel: forXinchRange (width): forYinchRange (height): Draw.point ((x, y), fill=Rndcolor ())#Output Text: forTinchRange (4): Draw.text ((* t + ten, Rndchar (), font=Font,fill=RndColor2 ())#Blur:Image =image.filter (Imagefilter.blur) im= Image.Save ('code.jpg','JPEG') M= Image.open ('code.jpg') m.show ()
The output is:
Python module: PIL