Python image processing (2) image watermarking and PiL mode transformation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.