Creating image, cropping and swapping images , making collage using python

Yukta chakravarty
3 min readJun 9, 2021

What is an image?

From Human’s point of view image is representation of how something looks like. Example: If we see a table we can tell how it looks like i.e its color, shape,size,etc

But for computers images are just numbers , we represent images in arrays having rows and columns and each cell is called as pixel, by giving values to pixels we create images.

Create image by yourself Using Python Code

We can create black and white images by giving values 0 and 1, for colored images we give value in RGB(RED, BLUE,GREEN) format

Here, we are using open cv so give values in BGR format

Just by manipulating the values of pixels we can create images

We are using Open CV here. numpy.zeros helps to initialize our array with zeros which will render black back image and we store it in variable named ‘cn’

To change background color use we change value of all pixels , we can easily do so using cn[: , :]=value, giving : in rows and columns it will change value of all pixels

cn[200:400,280:320], here 200:400 represents row and it corresponds to height of image while 280:320 here represents column and corresponds to width of image

We can apply different logic for different shapes and form images

Take 2 images, crop some part of both the images and swap them.

Here , we use imread function to read images whose output is an array

Then we crop images and store them in other variables then we swap faces of images

Above images are original images, below are images with faces switched

Take images and combine it to form a single image. For example, collage

For making collage we just have to replace value of pixels of background with image we want

Images for making collage

--

--