Guide to Image Compositing (With Python Codes)

Image Compositing

What is Compositing?

Compositing is a technique that compresses separate elements into one image. We can say that creating more details into one image by combining multiple images or editing in a single image. You’ve absolutely seen composite images every day – in ads, on websites, in the news, even on your friend’s social media. Sometimes in compositing, we combine different images into one image that are not even photographed in one place; it’s just like changing the sky with sea or something else present in different images. Many use image compositing cases; whenever required to remove, combine, or extract any object present inside an image, we use image compositing.  

Composited image of a footballer into an image of a stadium

 All compositing procedures are basically to replace the selected parts with other material of an image but not almost from an image; sometimes we need to add or remove components from other images also so this is basic image compositing, let’s get more acquainted about techniques like alpha blending and color mosaics in image compositing by using python.

What is Alpha Blending?

Alpha Blending is the process of creating an image partially or fully transparent by combining one image with a background. We often use it to render pixels(picture elements)in separate layers, and then a 2D image gets combined into a single or final image called composite. Alpha Blending is also used to put a rasterized foreground element over a background in 2D computer graphics.

To combine the pixels correctly, it is necessary to work on the associated matte for each element; in addition to its colour these matte layers exist with information like :

  •  The shape of geometry being drawn.
  • Difference between the part of the image

There are many blend modes. The basic operation of combining two images is to put one over the other.  

The math behind the alpha blending is simple, at the picture element (pixel) of any image, we need to combine foreground elements color (f) and background color(b) using the alpha mask.

Note: The value of \alpha used in the equation is the pixel value in the alpha mask divided by 255. So, in the equation below, 0 \leq \alpha \leq 1

I = \alpha F + (1 - \alpha) B
  1. When \alpha = 0, the output pixel color is the background.
  2. When \alpha = 1, the output pixel color is simply the foreground.
  3. When 0 < \alpha < 1 the output pixel color is a mix of the background and the foreground. For realistic blending, the alpha mask boundary usually has pixels between 0 and 1.

Alpha Blending using Python 

In this section, we would learn how to overlay a foreground imagery a background image

There are few steps to do that. 

Set up the environment in Colab

Requirements: Python 3.6 or above,  OpenCV-python 4.5 or above

  •  Importing the required libraries (OpenCV)
 import cv2
 from google.colab.patches import cv2_imshow 
  •  Read the image for blending 
 img1 = cv2.imread('/aim.png')
 img2 = cv2.imread('/mask.png') 
  • Viewing the images we have read 


  • Making a while loop.

By using a loop, we can iterate between many alpha values for a satisfactory result.

 while (choice) :
   alpha = float(input("Enter alpha value"))
   dst = cv2.addWeighted(img1, alpha , img2, 1-alpha, 0)
   cv2.imwrite('alpha_mask_.png', dst)
   img3 = cv2.imread('alpha_mask_.png')
   choice = int(input("Enter 1 to continue and 0 to exit")) 


  • Selecting and entering an alpha value between 0 to 1.

By using all those steps, we can make a mask with the logo of AIM, so that is just a task where I needed to make a mask with that logo with the help of blending technique where I extracted the mask from mask.png and the logo and colour of AIM.png and composited in an image.


You can refer to the code here.

What is Color Mosaics?

A mosaic is a pattern or image made by colored stone, usually small in size. We can take some examples from some old photographs made by colorful stones, marbles and glass pieces; many social media platforms are using image filters nowadays where we can see many of them related to mosaic. Apart from this, there are many uses of color mosaic. In a technical term, we can say most of the time; a target image is called a photomosaic; when it is made of a huge number of small rectangle grid images, grid images are already an image of any object. By combining those grids, we achieve a target image that looks like an image of a different object. The grid doesn’t need to be an image of different objects.

Two kinds of photo mosaic that depend on how the matching of the grid is done.

  • Simpler kind – In the target image, we average down grid images of a single color and reduce every section of a single color; these sections can be considered a library of images. Each grid of the target image is then replaced by the library, where these colors are available as similar as possible. Its resolution downsampling the target image, and then every resulting pixel is replaced with an image whose average color matches that pixel.
  •  In the more advanced kind of photographic mosaic, we don’t downsample the target image or reduce its resolution. For the matching, we compare each picture element in the rectangle grids to the corresponding picture element from each library image. Then we replace the rectangle in the target image with the library image, which minimises the total difference between picture elements, hence generating better results. This advance photographic mosaic requires much high computation than the simple kind. Still, the advantage of it is that the results can be much better since the pixel-by-pixel comparison and matching can secure the target image by downsampling or effect on its picture element.

So till now, we have done a theoretical discussion on how it happens or works. In the next section, we try to make an image photo mosaic using python and some libraries.

Breaking an image into its pixels and increasing its size is fundamental to the photomosaic. 

Simply, I am trying to do this with an image of a girl.

Here we are going to use the following libraries: 

  • Skimage
  • Matplotlib
  • Photomosaic
  • OpenCV
Set up the environment in Colab

Requirements: Python 3.6 or above, skimage 0.17.2 or above , matplotlib ,photomosaic 0.3.1,opencv-python 4.5 or above

  • Installing photomosaic 

!pip install photomosaic

  • Importing libraries 
 from import *
 import sys
 import photomosaic
 from skimage import data
 import matplotlib.pyplot as plt
 import cv2 
  • Reading the image and converting it into its original color.
 image = cv2.imread('/content/drive/MyDrive/Yugesh/lena.jpg')
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


  • Creating all image squares and generating a pool.
 square_pool = photomosaic.make_pool('square/*.png') 
 5832it [00:04, 1440.60it/s]
 analyzing pool: 100%|██████████| 5832/5832 [00:11<00:00, 509.66it/s] 
  • Creating the mosaic image. Providing image, square pool and grid size to mosaic 

mosaic = photomosaic.basic_mosaic(image, square_pool,(200, 300))


 partitioning: depth 0: 100%|██████████| 60000/60000 [00:00<00:00, 276188.17it/s]
 analyzing tiles: 100%|██████████| 60000/60000 [00:01<00:00, 47331.04it/s]
 matching: 100%|██████████| 60000/60000 [00:02<00:00, 21374.57it/s]
 drawing mosaic: 100%|██████████| 60000/60000 [00:05<00:00, 11733.51it/s] 
  • View of the image 



  • Let’s check it with different grid size.

This time we have provided (100,150), and the output is 

 And for (30,20)

It means as we increase the grid size, we won’t be able to see the image clearly because in the process, we are trying to replace similar color rectangles into the library where its best match is available. Here we tried to make grids of similar colors to understand the basics thoroughly. The further you go from the image, you will be able to see a clear image because of the distribution of colors in each of the grids and the distribution of similar color grids throughout the image.

We can check the codes for doing this here.


Download our Mobile App

Subscribe to our newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day.
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

Our Recent Stories

Our Upcoming Events

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox