The Wayback Machine - https://web.archive.org/web/20240717024239/https://www.geeksforgeeks.org/how-to-merge-a-transparent-png-image-with-another-image-using-pil/
Open In App

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

Last Updated : 03 Mar, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

This article discusses how to put a transparent PNG image with another image. This is a very common operation on images. It has a lot of different applications. For example, adding a watermark or logo on an image. To do this, we are using the PIL module in Python. In which we use some inbuilt methods and combine the images in such a way that it looks to be pasted.

  • Open Function – It is used to open an image.
  • Convert Function – It returns a converted copy of a given image. It converts the image to its true color with a transparency mask.
  • Paste Function – It is used to paste an image on another image. 

Syntax: PIL.Image.Image.paste(image_1, image_2, box=None, mask=None)
OR 
image_object.paste(image_2, box=None, mask=None)

Parameters:

  • image_1/image_object : It is the image on which other image is to be pasted.
  • image_2: Source image or pixel value (integer or tuple).
  • box: An optional 4-tuple giving the region to paste into. If a 2-tuple is used instead, it’s treated as the upper left corner. If omitted or None, the source is pasted into the upper left corner.
  • mask: a mask that will be used to paste the image. If you pass an image with transparency, then the alpha channel is used as the mask.

Approach:

  • Open the front and background image using Image.open() function.
  • Convert both the image to RGBA.
  • Calculate the position where you want to paste the image.
  • Use the paste function to merge the two images.
  • Save the image.

Input Data:

To input the data, we are using two images:

  • Front Image: A transparent image like a logo

  • Background image: For background like any wallpaper image

Implementation:

Python3




# import PIL module
from PIL import Image
  
# Front Image
filename = 'front.png'
  
# Back Image
filename1 = 'back.jpg'
  
# Open Front Image
frontImage = Image.open(filename)
  
# Open Background Image
background = Image.open(filename1)
  
# Convert image to RGBA
frontImage = frontImage.convert("RGBA")
  
# Convert image to RGBA
background = background.convert("RGBA")
  
# Calculate width to be at the center
width = (background.width - frontImage.width) // 2
  
# Calculate height to be at the center
height = (background.height - frontImage.height) // 2
  
# Paste the frontImage at (width, height)
background.paste(frontImage, (width, height), frontImage)
  
# Save this image
background.save("new.png", format="png")


Output:



Similar Reads

Create transparent png image with Python - Pillow
To create a transparent png using Python3, the Pillow library is used. The Pillow library comes with python itself. If python is unable to find Pillow library then open the command prompt and run this command:- pip install Pillow Note: If you got any problem installing Pillow using pip, then install and setup pip first. For this check this article.
3 min read
Python PIL | Image.merge() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images. Image.merge() Merge a s
1 min read
Convert OpenCV image to PIL image in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a
3 min read
How to make background image transparent using Python?
In this article, the task is to create a background transparent of the image in Python Library Required : First Install pillow library on your Python Application before going ahead. Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many ima
1 min read
Adding Text on Image using Python - PIL
In Python to open an image, image editing, saving that image in different formats one additional library called Python Imaging Library (PIL). Using this PIL we can do so many operations on images like create a new Image, edit an existing image, rotate an image, etc. For adding text we have to follow the given approach. Approach Import moduleOpen ta
2 min read
Convert Blob Image to PNG and JPG Using Python
We are given a task to convert blob images to png and jpg with Python. In this article, we will see how we can convert blob images to PNG and JPG with Python. Convert Blob Image to PNG and JPG With PythonBelow are step-by-step procedures by which we can convert blob images to PNG and JPG with Python: Step 1: Create a Virtual EnvironmentWe will crea
3 min read
Python PIL | Image.crop() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.crop() method is used to crop a rectangular portion of any image. Syntax: PIL.Image.crop(box = None)Parameters: box - a 4-tuple defining the left, upper, right, and lower pixel coordinate.Return type: Image (Returns a rectangular regio
2 min read
Python PIL | Image.new() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.new() method creates a new image with the given mode and size. Size is given as a (width, height)-tuple, in pixels. The color is given as a single value for single-band images, and a tuple for multi-band images (with one value for each
2 min read
Python PIL | Image.split() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. Image.split() method is used to split the image into individual bands. This method returns a tuple of individual image bands from an image. Splitting an “RGB” image creates three new images each containing a copy of one of the original bands (re
1 min read
Python PIL | Image filter with ImageFilter module
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method. Image used: Filters - The current version of the library provides the set of predefined image enhancement filters: 1.
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg