How to merge a transparent PNG image with another image using PIL? (original) (raw)

Last Updated : 23 Jul, 2025

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.

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

Parameters:

Approach:

Input Data:

To input the data, we are using two images:

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: