Python PIL image blend() method

Many times we need to merge two images into a single image for giving it some special effects, therefore we need to know how to blend these two images in Python or merge them.

So In this tutorial, we are going to learn how to blend or merge images using Python. In this, we are going to use the Python Imaging Library (PIL), which is also known as ‘Pillow’. In Pillow, we are going to use the ‘Image’ Module as it consists of the ‘Blend’ method that blends two images.

About blend() Method in Python

This function returns a new image by interpolating between two input images. An important thing to note is that both the images must be of the same size and same mode, that is there width and height must be similar and their modes such as RGB, RGBA, CMYK, etc must be the same. It also takes a third parameter as an input that is the ‘Alpha’ factor, Now this plays an important role in interpolating two images. If the value of alpha is ‘0.0’ then first input image is returned, if its value is ‘1.0’ then second input image is returned and if its value is between ‘0.0’ and ‘1.0’ then mixture or blend of two images would be returned depending upon the inclination of alpha value.

The Mathematical Equation used in the Blend() method:

out = image1*(1.0 - alpha) + image2*alpha

To learn more about Pillow and Image module you can refer to there documentation.

Getting Started with Code

Before getting started with actual code we need to install the required libraries.


$ pip3 install Pillow

Now, comes the source code, I would suggest you go through the code thoroughly then afterward I’ll be explaining the necessary details.

Source Code: blend() Method in Python

# Importing Required Module
from PIL import Image

# Creating a image1 object
image1 ="img1.jpg")

# Creating a image2 object
image2 ="img2.jpg")

# As Alpha value is 0.0, Image1 would be returned
image3 = Image.blend(image1,image2,0.0)"output1.jpg")

# As Alpha value is 0.5, Blend of both would be returned
image4 = Image.blend(image1,image2,0.5)"output2.jpg")

# As Alpha value is 1.0, Image2 would be returned
image5 = Image.blend(image1,image2,1.0)"output3.jpg")

In this firstly, I had imported the required libraries, then I had created two images objects consisting of two different images. Here, both the images are of the same size and same mode, if you are using the image of different sizes or mode, first of all, convert them into the same size and mode, then use them for interpolation. Lastly, I had produced three outputs with varying value of alpha between ‘0.0’ and ‘1.0’.


Output 1, when alpha value is 0.0, it returned image1:

Python PIL image blend() method

Output2, when the alpha value is 0.5, it returned blend of both the images:

Python PIL image blend() method

Output3, when the alpha value is 1.0, it returned image2:

blend() method

So In this way, we can blend two images using PIL image blend() method in Python. I hope this tutorial would be helpful to you, Thank you ‘Keep Learning Keep Coding’.

Leave a Reply

Your email address will not be published. Required fields are marked *