How to merge a transparent PNG image with another image using PIL?


In the world of image processing, merging two or more images together is a common operation. One common use case is to merge a transparent PNG image with another image to create a composite image that contains both images.

In this article, we will learn how to merge a transparent PNG image with another image using PIL. PIL is a powerful library for working with images in Python. It provides a range of functions for opening, manipulating, and saving different types of image files. The library is compatible with a wide range of image formats, including JPEG, PNG, BMP, GIF, and TIFF.

In order to merge a transparent PNG image with another image using PIL, we will need to perform the following steps:

  • Load the two images into PIL using the Image.open() function

  • Create a new image with the same size as the background image

  • Paste the background image onto the new image using the paste() function

  • Paste the foreground image onto the new image using the paste() function, but with a mask that indicates which pixels should be transparent

  • Save the merged image to a file using the save() function

Let’s now see how to merge a transparent PNG image with another image using PIL stepwise in detail:

Step 1: Load the images into PIL

The first step is to load the background image and the transparent PNG image into PIL. We can do this using the Image.open() function, which takes the filename of the image as an argument. For example, to load a background image called "background.jpg" and a transparent PNG image called "foreground.png", we can use the following code:

from PIL import Image
background = Image.open("background.jpg")
foreground = Image.open("foreground.png")

Step 2: Create a new image with the same size as the background image

Next, we need to create a new image that has the same size as the background image. We can do this by calling the Image.new() function and passing in the size of the background image. For example, if the background image is 800x600 pixels, we can create a new image with the same size like this:

merged_image = Image.new("RGBA", background.size)

Step 3: Paste the background image onto the new image

Now we can paste the background image onto the new image using the paste() function. This function takes the following arguments:

  • The image to be pasted (in this case, the background image)

  • The location to paste the image (specified as an (x, y) tuple)

  • An optional mask that can be used to control the transparency of the pasted image

For example, to paste the background image onto the new image at the top left corner, we can use the following code:

merged_image.paste(background, (0, 0)

Step 4: Paste the foreground image onto the new image with a mask

Now we need to paste the transparent PNG image onto the new image, but with a mask that indicates which pixels should be transparent. We can create a mask from the alpha channel of the foreground image using the split() function, like this

_, _, _, mask = foreground.split()

Step 5: Save the merged image to a file

Finally, we can save the merged image to a file using the save() function. We can specify the filename and file format using the appropriate arguments. For example, to save the merged image as a JPEG file called "merged.jpg", we can use the following code:

merged_image.save("merged.jpg", "JPEG")

Example

Merging of a Transparent PNG with a JPEG Background Image

In the below example, we merge a transparent PNG image with a JPEG background image using the PIL library in Python. It involves loading the images into PIL, creating a new image with the same size as the background image, pasting the background image onto the new image, creating a mask from the alpha channel of the foreground image, and pasting the foreground image onto the new image with the mask. Finally, the merged image is saved to a file.

# import PIL module
from PIL import Image
# Load the images
myBackgroundImage = Image.open("yourbackground.jpg")
myForegroundImage = Image.open("yourforeground.png")
# Create a new image with the same size as the background image
myMerged_image = Image.new("RGBA", myBackgroundImage.size)
# Paste the background image onto the new image
myMerged_image.paste(myBackgroundImage, (0, 0))
# Create a mask from the alpha channel of the foreground image
_, _, _, mask = myForegroundImage.split()
# Paste the foreground image onto the new image with the mask
myMerged_image.paste(myForegroundImage, (0, 0), mask)
# Saving the merged image to a file
myMerged_image.save("yourmerged.jpg", "JPEG"

Output

Input Images

Output Images

Example

Merging Multiple Transparent PNG Images

In the below example, we have merged multiple transparent PNG images using the PIL library in Python. It involves loading the images into PIL, creating a new image with the same size as the background image, pasting the background image onto the new image, creating a mask from the alpha channel of each foreground image, and pasting each foreground image onto the new image with its respective mask. Finally, the merged image is saved to a file.

# import PIL module
from PIL import Image

# Loading the images
myBackgroundImage = Image.open("yourbackground.jpg")
myForegroundImage1 = Image.open("yourforeground1.png")
myForegroundImage2 = Image.open("yourforeground2.png")

# Create a new image with the same size as the background image
myMerged_image = Image.new("RGBA", myBackgroundImage.size)

# Paste the background image onto the new image
myMerged_image.paste(myBackgroundImage, (0, 0))

# Create a mask from the alpha channel of the foreground image
_, _, _, mask1 = myForegroundImage1.split()
_, _, _, mask2 = myForegroundImage2.split()

# Paste the foreground image onto the new image with the mask
myMerged_image.paste(myForegroundImage1, (0, 0), mask1)
myMerged_image.paste(myForegroundImage2, (0, 0), mask2)

# Saving the merged image to a file
myMerged_image.save("yourmerged.png", "PNG")

Output

Input Images

Output Image

Conclusion

Merging a transparent PNG image with another image is a common task in image processing. In this article, we learned how to use the Python Imaging Library (PIL) to merge a transparent PNG image with a background image. We covered the steps of loading the images into PIL, creating a new image with the same size as the background image, pasting the background image onto the new image, creating a mask from the alpha channel of the foreground image, pasting the foreground image onto the new image with the mask, and saving the merged image to a file. With these techniques, you can create complex composite images that combine multiple images with transparency.

Updated on: 31-Aug-2023

912 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements