Creating custom images by python Code.

Ansh Kumar Dev
2 min readJun 5, 2021

So how we can create our own custom image using python?
for this, we will need few libraries :

Numpy & OpenCV

Image is basically a 3D array so first, we will create our own 3d array with data type “uint8” using the Numpy zeros() function. It will create custom 3D arrays of zeros with the given dimensions.

Now as we have our array we can change its pixel values to get a color pattern.

We have used cv2 rectangle function here for making pattern in the array. Once the pattern is made we can show the image using opencv imshow() function.

Croping and swapping two parts of images

First we need to crop our Images.

For this, we are going to manually select the dimensions of the cropped image we want.

Once we have both the cropped images. We can assign the new cropped image dimensions to one of the previous image on the same place from which we have cropped.

Combining two images to form one

It can be very easily done.

first, we will take two images and match their dimensions.

If the dimensions don't match we can manually adjust them. Once the images have the same dimensions from which they have to be combined. we can stack them using numpy’s hstack() or vstack() functions.