”;
What is compositing Image?
Compositing images in Pillow involve combining two or more images to create a new image. This process often includes blending the pixel values of one image with another based on certain criteria such as transparency or specific blending modes.
Pillow provides the Image.alpha_composite() method for compositing images especially when dealing with alpha (transparency) channels. It is commonly used for overlaying one image onto another adding watermarks or creating special effects.
Here are the key concepts related to compositing images in Pillow −
Image Composition
-
Combining images to create a new image by overlaying one image onto another.
-
The image composting is commonly used for adding watermarks or creating special effects.
Alpha Channel
-
The alpha channel represents the transparency of each pixel in an image.
-
Images with an alpha channel can be composited more seamlessly allowing for smooth blending.
The Image.alpha_composite() Method
The Image.alpha_composite() method in Pillow is used for compositing two images using their alpha channels. It takes two Image objects as input and returns a new Image with the composited result.
Here is the syntax and parameters of the Image.alpha_composite() method −
PIL.Image.alpha_composite(image1, image2)
Where,
-
image1 − The background image onto which the second image will be composited.
-
image2 − The foreground image that will be composited onto the background.
Example
This example shows how to composite two images using Image.alpha_composite() method.
from PIL import Image #Open or create the background image background = Image.open("Images/decore.png") #Open or create the foreground image with transparency foreground = Image.open("Images/library_banner.png") #Ensure that both images have the same size if background.size != foreground.size: foreground = foreground.resize(background.size) #Perform alpha compositing result = Image.alpha_composite(background, foreground) # Display the resulting image result.show()
Image to be used
Output
Adding Watermarks Using Compositing Images
Adding a watermark to an image is one of the applications in image compositing tasks. You can overlay a watermark onto an image at a specific position and transparency. This is achieved by creating a new layer for the watermark and blending it with the base image using the Image.alpha_composite() method.
Example
This example demonstrates how to to add a watermark onto an image using the Image.alpha_composite() method. The watermark image is placed on top of the base image with adjustable transparency.
from PIL import Image # Load the images and convert them to RGBA image = Image.open(''Images/yellow_car.jpg'').convert(''RGBA'') watermark = Image.open(''Images/reading_img2.png'').convert(''RGBA'') # Create an empty RGBA layer with the same size as the image layer = Image.new(''RGBA'', image.size, (0, 0, 0, 0)) layer.paste(watermark, (20, 20)) # Create a copy of the layer and adjust the alpha transparency layer2 = layer.copy() layer2.putalpha(128) # Merge the layer with its transparent copy using the alpha mask layer.paste(layer2, (0, 0), layer2) # Composite the original image with the watermark layer result = Image.alpha_composite(image, layer) # Display the resulting image result.show()
Image to be used
Watermark Image
Output
”;