Python pil for picture compositing

Source: Internet
Author: User
Tags transparent image

You need to fit two pictures together in your project. In two cases, one is the synthesis of two non-transparent images, and one is the synthesis of transparent PNG.

Related APIs See http://pillow.readthedocs.io/en/latest/reference/Image.html

In the first case, it is possible to combine two pictures directly. Combine two pictures as shown

+        =

Detailed code

     fromPILImportImage#Loading Basemapsbase_img = Image.open (ur'D:\Desktop\1.png')    #You can view the size and mode of the picture, and the common mode has RGB and Rgba,rgba more alpha transparency than RGB    #print base_img.size, Base_img.modebox = (166, 64, 320, 337)#areas on the basemap that need p off    #loading a picture that requires p to go uptmp_img = Image.open (ur'D:\Desktop\2.png')    #Here you can select an area or an entire picture    #Region = Tmp_img.crop ((0,0,304,546)) #选择一块区域    #or use the entire pictureRegion =tmp_img#Use the Paste (region, box) method to paste the picture into another image.    #Note that the size of region must match the size of box exactly. But the mode of the two pictures can be different, the merging of the time back to automatic conversion. If you need to preserve transparency, use the RGMA mode    #zoom the picture in advance to fit the box area size    # region = region.rotate (+) #rotate a pictureRegion = Region.resize ((box[2]-box[0], box[3]-box[1])) base_img.paste (Region, Box)#base_img.show () # View a picture of a compositionBase_img.save ('./out.png')#Save Picture

In the first case, a regular picture is merged, such as a rectangle, which is difficult for any shape to picture. For any shape merge, consider using a transparent PNG to complete.

In the second case, place the non-transparent image on the bottom, place a local transparent PNG image, and then synthesize a picture.

Code

     fromPILImportImage#loading the middle transparent phone picturebase_img = Image.open (ur'D:\Desktop\3.png')    #new transparent Basemap, same size as the phone graph, mode uses RGBA, retains alpha transparency, and is transparent in color    #image.new (mode, size, color=0), color can be expressed as a tuple, representing the value of RGBA, respectivelytarget = Image.new ('RGBA', Base_img.size, (0, 0, 0, 0)) box= (166, 64, 320, 337)#Area    #loading needs fox likeregion = Image.open (ur'D:\Desktop\4.png') Region= Region.rotate (180)#Rotate 180 degrees    #Make sure the image is in RGBA format, the same size as the box areaRegion = Region.convert ("RGBA") Region= Region.resize ((box[2]-box[0], box[3]-box[1]))    #First, the fox is like a synthetic image.target.paste (Region,box)#cover the phone map, the middle transparent area to show the fox like. Target.paste (Base_img, (0,0), base_img)#The first parameter represents the image that needs to be pasted, coordinates in the middle, and finally a mask picture that specifies the transparent area to display the Basemap.     #target.show ()Target.save ('./out.png')#Save Picture

Python pil for picture compositing

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.