Python for Art – Blending Two Images using OpenCV

In this article, I will show you how create nice looking custom designs using Python. There are many designing tools out there, but in this project our goal is to create a beautiful design without using those softwares. After working on this project, you will also have an idea on how to work with OpenCV package. It is a great skill to have, especially if you like computer vision. You can follow a similar structure when working on similar OpenCV projects.

I love art and doing design work, and in this project I wanted to combine my passion with programming. This is actually one of the biggest reason I enjoy programming, it gives you the opportunity to work on a very different areas of interest. I see programming like a pencil and the text editor as a blank paper. It all starts with your imagination skills. The rest comes as you start writing the code. Python makes this process much smoother and fun. Enough with introduction, let’s get started!

Table of Contents:

  • Python
  • Getting Started
  • Step 1 – Reading Images
  • Step 2 – Resizing Images
  • Step 3- Blending Images
  • Final Step – Exporting the Result

Python

Python is a general-purpose programming language that is becoming ever more popular for analyzing data. Python also lets you work quickly and integrate systems more effectively. Companies from all around the world are utilizing Python to gather bits of knowledge from their data. The official Python page if you want to learn more.


Getting Started

We will use just one python package for this project. As you can understand from the title, it is called OpenCV. OpenCV is a must have package when working with computer vision projects. Here is a nice short definition of OpenCV:

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

https://opencv.org

We have to install this library so that we can use it in our program. And for OpenCV to work fine, we have to install numpy library too. We can install them in one line using the pip python library manager:

pip install numpy opencv-python

After the installation is completed, let’s import them into our code editor. For this project, I will be using Jupyter Notebook. Feel free to use your preferred programming environment. The OpenCv is imported as cv2 as following:

import cv2

Great! Now we can move to the next step, where we will define the two images that we will use to blend them on each other. More about this process in the next step.


Step 1 – Reading Images

First things first, ask yourself this question: what kind of blending art do you want to make? There are many combinations you can try. You can combine two different pictures, or a text image with an image, or colored background with an image. In this exercise, I will blend a text image with a nice background image. Let me show you the two images that I will be working on:

Well, now let’s import them into our program. I created a folder and renamed it “images”. It is inside the same folder with my notebook file. Here is the code to define images using OpenCV.

bg = cv2.imread('images/background.jpg', cv2.IMREAD_COLOR)
fg = cv2.imread('images/my_brand.png', cv2.IMREAD_COLOR)

Step 2 – Resizing Images

In this step, we will resize the images that we want to blend. This step can be also called preprocesseing the images. We are resizing the images to make sure they are in the same size. Same size is required for the blend function to work properly. Otherwise, it will return an error message.

Before resizing them, let me show you their original sizes.

As you can see, the background image is 853 to 1280 pixels. And the foreground image is 1440 to 2560 pixels. We will resize them using the resize function by OpenCV.

dim = (1200, 800)

resized_bg = cv2.resize(bg, dim, interpolation = cv2.INTER_AREA)
resized_fg = cv2.resize(fg, dim, interpolation = cv2.INTER_AREA)

Cool! Now, our images are in the same size. We can move to the next step, where we will start the blending process. In other words, the fun part of this whole article.


Step 3 – Blending Images

Thanks to OpenCV, we can do it in one line of code. The function that will do the blending for us is called addWeighted. You can learn more about addWeight function from here. It has 5 parameters, which can be listed as: image source 1, src1 weight, image source 2, src2 weight, gamma. The weight of each image has to be a value less than 1. Here is the blend equation:

blend = (image scr1)*(src1 weight) + (image scr2)*(src2 weight) + gamma

That’s the mathematics of the function. Let’s see in action. I gave a little more weight to the foreground. This way the background will be darker, and the text can be more brighter.

blend = cv2.addWeighted(resized_bg, 0.5, resized_fg, 0.8, 0.0)

So far so good, the blending is completed. Now let’s move to the next step to export our final work.


Final Step – Exporting the Result

You made it until this step, you are magnificent! Now, let’s export the final work by using the imwrite method. Here is the line to save the image as a new image file in the folder.

cv2.imwrite('blended.png', blend)
final design

Congrats!! You have a created a program that blends two different images in one final design. Now, you have some understanding of how OpenCV (Open Source Computer Vision) library is being used in a real project. Hoping that you enjoyed reading this article and working on this project. I am planning to continue this series with more art and programming blending projects 🙂

Working on hands-on programming projects like this one is the best way to sharpen your coding skills. Feel free to contact me if you have any questions while implementing the code.

Follow my blog and YouTube channel to stay inspired. Thank you,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s