Drawing and Cropping image using Python code

Garvit Garg
4 min readJun 9, 2021

Hey guys in this block we are gonna draw, crop and create collage through python code.
For this we are gonna use :
Python as language
Jupyter notebook
2 images with a face

Drawing any Image:
When we look to any image in phone or laptop it seems as if there is some high graphic tech. used for storing that image.
But actually an image is stored in any system in form of array.
So by use of these arrays only we are gonna create a hut on a blank black sheet.
So lets create a hut on a blank black sheet of shape — (1000, 1000, 3)
For creating a hut we need to use cv2 library of python.
So firstly import library cv2.
Now lets upload our empty sheet in our program
Or we can create our own image using numpy library.
After uploading lets check our sheet saved by name “test”

Our sheet is now like this :

As we can see shape of our image is = (1000, 1000, 3)
Which means this pic is totally divided into 1000 rows and 1000 columns.

Now lets use concept of arrays to draw a hut.
To draw straight lines of a hut we use :

Now if we see our image it looks like :

Now our only task left is to draw all triangular straight line so for that we can use the code as :

Now if we see our image it looks like :

Now if we want to put colors in this image we can use the code as :

Now our final image created will be :

Cropping the Images :

In this part we will try to crop two images and then exchange the cropped part of each image.
For this we need to firstly upload two images in our RAM.

First photo with shape — (1037, 1182, 3) is :

Second photo2 with shape —(927, 1177, 3) is :

Now lets crop our first pic by using code :

The cropped pic will be :

Now let’s crop second image :

The cropped image is :

Now we have both the pics cropped and cropped part is stored in different folders named tomHolland and Robert.

Now lets put the cropped image2 on first image.
If we put Robert on photo1 we need code as :

Now photo will look like :

So this is how cropping is done.

--

--