Mode conversion:
PIL mode conversion: Converting pictures to other modes
1 #we convert image image to grayscale image (Python)2 fromPILImportImage3img = Image.open ('c:\\1.jpg')4Img.show ()#Open display of images5 #L = I.convert (' l ')6 #nine different modes of PIL: 1,l,p,rgb,rgba,cmyk,ycbcr,i,f7 #Mode Conversion Statements8L = Img.convert ('I')9W,h = Img.size#Figure SizeTenmode = Img.mode#the pattern of the picture OneGeSHi = Img.format#the format of the picture A Print(W,h,mode,geshi) -L.show ()
Watermarking:
fromPILImportImage, Imagedraw, Imagefont#Add a text watermark#Rgba means (Red-green-blue-alpha) that it is extended on RGB including the "Alpha" channel, which runs on the color value setting transparencyim = Image.open ("c:/1.jpg"). CONVERT ('RGBA') txt=image.new ('RGBA', Im.size, (0,0,0,0))#the size of the imageFnt=imagefont.truetype ("C:/windows/fonts/candara.ttf", 50)#Select the font for the watermarkD=imagedraw.draw (TXT) d.text (txt.size[0]-120,TXT.SIZE[1]-50),"Hello", Font=fnt,fill= (255,0,0,255)) out=image.alpha_composite (Im,txt) out.show () Out.save (R"C:/pp.png")
#Add a picture watermarkim= Image.open ("c:/2.jpg") Mark=image.open ("c:/1.jpg")Print(im.size)#RGBA indicates that four properties a represents transparency about new (' pattern ', size, color)Layer=image.new ('RGBA', Im.size, (0,0,0,0)) Layer.paste (Mark, (im.size[0)-400,IM.SIZE[1]-200))#Change the location of the watermark image#Composite: Use two pictures given and a mask parameter similar to the Alpha parameter whose value can be: "1", "L", "RGBA". The size of the two pictures must be the same. out=image.composite (Layer,im,layer) out.show ()
#参考 50747084
Python image processing (2) image watermarking and PiL mode transformation