Python CV2 — Create, Crop and Collage.

Hema R
3 min readJun 9, 2021

Hello everyone,

In this article we are going to see how to create an image using python, cropping the image and swapping it and finally how to collage using python.

github url : https://github.com/Hema-23062001/Python-Cv2.git

Create an image:

To create an image by using python, first we need to import numpy and cv2 module.

After importing those modules. First let us create black background.

For this we use numpy.zeros().

Then by using cv2.circle(),cv2.line(),cv2.ellipse() I have created a doll image.

cv2.circle() arguments: image, center, radius, color,thickness.

cv2.line() arguments: image, point1, point2, color, thickness.

cv2.ellipse() arguments: image name, center, axes, angle, startAngle, endAngle, color, thickness.

Finally we can see our image using cv2.imshow().

Cropping image:

Now lets try to crop image. I have taken two images (a dog and a cat). Then images are loaded using cv2.imread() and to display the image we use cv2.imshow().

Now we try to crop their faces. For this we just need to specify the points from where we need to crop the photo.

For eg: f_crop=photo1[10:250,30:250]

Thats it now the dog face is cropped.

Similarly we crop the cat face also.

Now let us try to swap the cropped dog face to cat image.

For this we only need to assign the cropped part of dog image to cat image(we need to specify the points where the dog image have to be placed.).

Collage:

Finally we are going to collage two images.

First let us load a image.

Then the only thing we need to do is we have to use vconcat() or hconcat().

Final result for using cv2.hconcat(image1,image2).

Thanks for reading!!!

--

--