1. Preparation
Loading Images for Combining
Before jumping into working with multiple images, we need to load them. Pillow supports most popular image formats like JPEG, PNG, BMP, and GIF.
from PIL import Image
# Loading images
background = Image.open("background.jpg")
foreground = Image.open("foreground.png")
Here we load two images: a background and an overlay image. Keep in mind that for some methods, both images need to be the same size.
2. Overlaying Images
The paste() Method
The paste() method lets you place one image on top of another at specified coordinates. This method is useful for overlaying one image on another or creating a collage effect.
# Resizing the foreground to match the background size
foreground = foreground.resize(background.size)
# Overlaying the foreground on the background
background.paste(foreground, (50, 50)) # Placing foreground at coordinates (50, 50)
# Saving the result
background.save("pasted_image.jpg")
Here, the foreground image is placed onto the background starting at the coordinates (50, 50). If the foreground goes beyond the bounds of the background, it will be partially cropped.
Using a Mask with paste()
The paste() method also supports masks, which allow for partial overlaying of an image while preserving its transparent areas. A mask should be in L (grayscale) or RGBA mode.
# Creating a mask
mask = Image.open("mask.png").convert("L") # Converting mask to grayscale
# Overlaying the image with the mask
background.paste(foreground, (50, 50), mask=mask)
background.save("pasted_with_mask.jpg")
The mask mask.png determines which parts of the foreground image will be visible, allowing for a smooth overlay onto the background.
3. Blending Images
The blend() Method
The blend() method allows you to mix two images with a specified transparency. It creates a new image object representing a combination of the two input images. To use blend(), both images must be the same size.
Simple Image Blending
# Resizing images to the same size
background = background.resize((500, 500))
foreground = foreground.resize((500, 500))
# Blending images with a transparency factor
blended_image = Image.blend(background, foreground, alpha=0.5)
# Saving the result
blended_image.save("blended_image.jpg")
The alpha parameter determines the transparency level of the foreground image. If alpha = 0.5, the images blend evenly. A value of alpha = 0.0 makes the foreground completely transparent (only the background is visible), while alpha = 1.0 makes the background completely transparent (only the foreground is visible).
4. Compositing Images with a Mask
The composite() Method
The composite() method lets you combine two images based on a mask, determining which parts of which image will be visible. This method is super handy for creating complex compositions and precisely overlaying one image on another.
Example: Using composite() to Combine Images
# Ensuring both images and the mask are the same size
background = background.resize((500, 500))
foreground = foreground.resize((500, 500))
mask = mask.resize((500, 500))
# Creating a composition with the mask
composited_image = Image.composite(foreground, background, mask)
# Saving the image
composited_image.save("composited_image.jpg")
The mask determines which parts of the foreground and background will be visible in the final image. White areas of the mask show the foreground, while black areas show the background.
5. Examples
Example of Combined Use of paste(), blend(), and composite()
Now let's combine these methods to create a more complex composition. Say we have a background, a semi-transparent image, and a mask.
from PIL import Image
# Loading images
background = Image.open("background.jpg").resize((500, 500))
foreground = Image.open("foreground.png").resize((500, 500))
mask = Image.open("mask.png").convert("L").resize((500, 500))
# 1. Blending the background and foreground with transparency
blended_image = Image.blend(background, foreground, alpha=0.3)
# 2. Overlaying with a mask on the final image
final_composite = Image.composite(foreground, blended_image, mask)
# Saving the final result
final_composite.save("final_composition.jpg")
This example shows how you can sequentially apply blend() and composite() to create layered compositions.
Practical Applications of Image Merging Methods
- Creating Collages: The
paste()method allows you to add multiple images to a single canvas, creating collages and mosaics. - Overlay and Transparency Effects: The
blend()method lets you create smooth transitions between images, which is super useful for photo editing and effects. - Compositions with Masks: The
composite()method allows precise control over which parts of which image are visible, enabling complex compositions using masks.
Comparison of Methods
| Method | Description | Application |
|---|---|---|
paste() |
Overlay one image on top of another | Collages, placing one image on top of another |
blend() |
Mix two images with specified transparency | Smooth transitions and overlay effects |
composite() |
Combine two images using a mask | Complex compositions and precise overlaying |
GO TO FULL VERSION