Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to merge a transparent PNG image with another image using PIL?
In image processing, merging a transparent PNG image with another image is a common operation. This creates composite images where the transparent PNG overlays seamlessly on top of a background image.
In this article, we will learn how to merge a transparent PNG image with another image using PIL (Python Imaging Library). PIL is a powerful library for working with images in Python, supporting various formats including JPEG, PNG, BMP, GIF, and TIFF.
Basic Steps for Image Merging
To merge a transparent PNG image with another image using PIL, we need to follow these steps ?
Load both images using
Image.open()Create a new image with the same size as the background
Paste the background image onto the new image
Extract the alpha channel from the PNG as a mask
Paste the foreground image with transparency using the mask
Save the merged result
Loading Images
First, we load both the background and foreground images into PIL objects ?
from PIL import Image
background = Image.open("background.jpg")
foreground = Image.open("foreground.png")
Creating the Composite Image
We create a new RGBA image with the same dimensions as the background image ?
merged_image = Image.new("RGBA", background.size)
Pasting Images with Transparency
The key to merging transparent PNGs is using the alpha channel as a mask. We extract this using split() and use it when pasting ?
# Paste background first
merged_image.paste(background, (0, 0))
# Extract alpha channel as mask
_, _, _, mask = foreground.split()
# Paste foreground with transparency
merged_image.paste(foreground, (0, 0), mask)
# Save the result
merged_image.save("merged.jpg", "JPEG")
Example 1: Basic PNG Overlay
Here's a complete example that merges a transparent PNG with a JPEG background ?
from PIL import Image
# Create sample images for demonstration
# Background image (solid color)
background = Image.new("RGB", (300, 200), color="lightblue")
# Foreground with transparency (circle with transparent background)
foreground = Image.new("RGBA", (100, 100), color=(255, 0, 0, 0))
from PIL import ImageDraw
draw = ImageDraw.Draw(foreground)
draw.ellipse([10, 10, 90, 90], fill=(255, 0, 0, 255))
# Create merged image
merged_image = Image.new("RGBA", background.size)
# Paste background
merged_image.paste(background, (0, 0))
# Extract alpha mask and paste foreground
_, _, _, mask = foreground.split()
merged_image.paste(foreground, (50, 50), mask)
print("Images merged successfully!")
print(f"Final image size: {merged_image.size}")
print(f"Final image mode: {merged_image.mode}")
Images merged successfully! Final image size: (300, 200) Final image mode: RGBA
Example 2: Multiple PNG Overlays
You can also merge multiple transparent PNG images onto a single background ?
from PIL import Image, ImageDraw
# Create background
background = Image.new("RGB", (400, 300), color="white")
# Create first transparent PNG (red circle)
png1 = Image.new("RGBA", (80, 80), color=(255, 255, 255, 0))
draw1 = ImageDraw.Draw(png1)
draw1.ellipse([5, 5, 75, 75], fill=(255, 0, 0, 200))
# Create second transparent PNG (blue rectangle)
png2 = Image.new("RGBA", (60, 100), color=(255, 255, 255, 0))
draw2 = ImageDraw.Draw(png2)
draw2.rectangle([5, 5, 55, 95], fill=(0, 0, 255, 150))
# Merge all images
merged_image = Image.new("RGBA", background.size)
merged_image.paste(background, (0, 0))
# Paste first PNG
_, _, _, mask1 = png1.split()
merged_image.paste(png1, (50, 50), mask1)
# Paste second PNG
_, _, _, mask2 = png2.split()
merged_image.paste(png2, (200, 100), mask2)
print("Multiple PNGs merged successfully!")
print(f"Background size: {background.size}")
print(f"PNG1 size: {png1.size}")
print(f"PNG2 size: {png2.size}")
Multiple PNGs merged successfully! Background size: (400, 300) PNG1 size: (80, 80) PNG2 size: (60, 100)
Key Points
Always use
RGBAmode for the merged image to preserve transparencyThe alpha channel serves as a mask for transparency
Use
split()to extract the alpha channel from PNG imagesThe third parameter in
paste()is the transparency maskSave as PNG to preserve transparency, or JPEG for flattened output
Conclusion
Merging transparent PNG images with background images using PIL involves extracting the alpha channel as a mask and using it during the paste operation. This technique allows you to create professional composite images while preserving transparency effects.
