This article studies the main python PIL implementation of image compositing is described below, sharing example code.
In the project, we need to combine two images together. Two situations are encountered, one is the compositing of two non-transparent images, and the other involves the compositing of a transparent png.
See /en/latest/reference/ for related APIs.
In the first case, you can directly combine the two images together. As shown in the figure below, combine the two images together
+
=
Detailed Code
from PIL import Image #Load the basemap base_img = (ur'D:\Desktop\') # You can view the size and mode of the image, common modes are RGB and RGBA, RGBA has more Alpha transparency than RGB. # print base_img.size, base_img.mode box = (166, 64, 320, 337) # Areas on the base map that need to be P'd out #Load the image that needs to be P'd up tmp_img = (ur'D:\Desktop\') # Here you can select an area or the whole image #region = tmp_img.crop((0,0,304,546)) #select a region # Or use the whole picture region = tmp_img # Use the paste(region, box) method to paste an image into another image. # Note that the size of the region must exactly match the size of the box. However, the two images can have different modes, which are automatically converted when merging. If you want to preserve transparency, use RGMA mode. # Scale the image ahead of time to fit the size of the box area # region = (180) #Rotate the image region = ((box[2] - box[0], box[3] - box[1])) base_img.paste(region, box) #base_img.show() # View the composite image base_img.save('./') #Save Image
In the first case, it is difficult to merge regular images, such as rectangles, with arbitrary shapes. For merging arbitrary shapes, you can consider using transparent PNG to accomplish this.
In the second case, put a non-transparent image at the bottom, a partially transparent png image on top, and composite a single image.
coding
from PIL import Image #Load cell phone image with transparent center base_img = (ur'D:\Desktop\') # Create a new transparent base image, the same size as the phone image, use RGBA for the mode, retain Alpha transparency, and the color is transparent #(mode, size, color=0), color can be represented by tuples, which represent RGBA values respectively target = ('RGBA', base_img.size, (0, 0, 0, 0)) box = (166, 64, 320, 337) # Region # Load needs a fox like # region = (ur'D:\Desktop\') region = (180) # Rotate 180 degrees # Make sure the image is in RGBA format and is the same size as the box area region = ("RGBA") region = ((box[2] - box[0], box[3] - box[1])) # First composite the fox image onto the base map # (region,box) # Overlay the cell phone image on it and the transparent area in the center will show the fox image. (base_img,(0,0),base_img) # The first parameter indicates the image to be pasted, the middle one is the coordinates, and the last one is the mask image, which is used to specify the transparent area to show the bottom image. # () ('./') # Save Image
summarize
Above is this article on python PIL to achieve the picture synthesis example code of all content, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!