Friday, December 27, 2024
Google search engine
HomeLanguagesOverlay an image on another image in Python

Overlay an image on another image in Python

Overlaying an image over another refers to the process of copying the image data of one image over the other. Overlaying could refer to other types of image processing methods as well such as overlaying similar images for noise reduction, Blending, etc. But for now, we will concentrate on the former one. In this article, we will learn how to overlay an image on top of another image using Image processing.

Module required:

Pillow: Python Imaging Library (expansion of PIL) is the de facto image processing package for the Python language. It incorporates lightweight image processing tools for editing, creating, and saving images.

pip install pillow

For demonstration purposes, we would be using the following image as the primary image.

Example 1: Overlaying an alpha image.

If we overlay an image containing transparent regions on an opaque image, then only opaque regions of the overlaid image would appear in the final image. The pixel may not be fully opaque and hence could have analog opacity (alpha channel). This type of overlay is the predominant one, as it allows for images to be blended in seamlessly.

For overlaying the image we would be using the paste() function found inside the pillow library.

Syntax: paste(self, im, box=None, mask=None)

   Pastes another image into this image.

Parameters:

  • im: 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: An optional mask image.

For demonstration, we would be overlaying the following image:

Below is the implementation:

Python3




from PIL import Image
  
# Opening the primary image (used in background)
img1 = Image.open(r"BACKGROUND_IMAGE_PATH")
  
# Opening the secondary image (overlay image)
img2 = Image.open(r"OVERLAY_IMAGE_PATH")
  
# Pasting img2 image on top of img1 
# starting at coordinates (0, 0)
img1.paste(img2, (0,0), mask = img2)
  
# Displaying the image
img1.show()


Output:

Explanation:

Firstly we opened the primary image and saved its image object into variable img1. Then we opened the image that would be used as an overlay and saved its image object into variable img2. Then we called the paste method to overlay/paste the passed image on img1. The first argument is img2 which is the image object of the image containing transparent text. This image would be used for overlay. The second argument is a size 2 tuple denoting the coordinates of img1, where the img2 should be pasted. Since it is (0, 0), hence the second image would be pasted at the top left side of img1. The third argument is img2 which is passed to the mask parameter. It will specify the transparency mask, for img2. In the end we displayed the image.

Example 2: Overlaying a non-alpha image

If we overlay a fully opaque image on top of an opaque image, all pixels values of the overlaid image get retained in the final image. In this process pixel values of the background image get lost during the process (at the region occupied by the overlaid image).

We would be using the following image as the overlay image:

Below is the implementation:

Python3




from PIL import Image
  
img1 = Image.open(r"BACKGROUND_IMAGE_PATH")
img2 = Image.open(r"OVERLAY_IMAGE_PATH")
  
# No transparency mask specified, 
# simulating an raster overlay
img1.paste(img2, (0,0))
  
img1.show()


Output:

Explanation:

The code is largely the same as the previous one, so only the changed code is of interest to us. In the paste method call, we omitted the mask argument, this allows no transparency mask to be used for the overlay. Therefore, the image is simply copy-pasted onto img1.  Since the pixel values of img2 are copied as it is, the white background is also present in the output image. It gives the viewer a clue that the image is modified without much consideration in the quality of the final image, due to abrupt color changes found in the final image (this situation is mitigated when the overlay image contains transparent regions). 

RELATED ARTICLES

Most Popular

Recent Comments