Image Creating and Swapping & Combining 2 Images with Python

Image Creating and Swapping & Combining 2 Images with Python

Create your own Image

One may be thinking how can I create my own image without using any editing software's? So Images are just play of arrays (2D/3D) depending on color scheme. So will be using two libraries for this:

  • numpy
  • open-cv

So first we will import libraries. The we declare a variable for storing array of dimension 1200*900 with all entries 255. 255 is used to create an image of white background as (255,255,255) is a color scheme for white. Now we will assign certain parts of this white background image, colors with syntax:

variable[100:200,270:370]= (255,0,255)

And then after assigning colors, we will show this array as image using cv2 module in open-cv library. Refer the Image for full code.

CODE_rain.png

So the Output of this will be as :

Rainbow_man.jpg

Swapping & Combining 2 Images

In this we will take 2 images of similar sizes and swap the faces of two different person with each other. And after swapping those images, we will concatenate them on same window. For this we used 2 libraries and one face detection model :

  • open-cv
  • numpy
  • haarcascade_frontalface_default

First we will read 2 images using cv2.read(image_name) and then load the model for both face detection using model.detectMultiScale(image_variable). This will return 4 points and which will be used to select the face and store in a variable. We will resize these images as follows:

Screenshot (250).png

Now we will fit these faces in each others body and then concatenate in one image and show them in one window as follows:

Screenshot (251).png

The output will be as follows:

Screenshot (252).png