Python and OpenCV, how to play with Images

Rodrigo Ancavil
3 min readMar 28, 2024

--

Nowadays, everything can be represented as Data, and photographs and images are no exception. So, we can use images like regular data and apply any analysis, filter, or whatever to them.

In this mini-article, we will write a simple example using OpenCV and Python to create an application that reads an image. We will then play with it, cropping a piece of the original image and applying a simple filter to modify each pixel. We’ll work on the cropped image like a normal numpy ndarray for this.

The final result will be the following.

First of all, we need to install opencv-python.

$ pip install opencv-python

That is all, and now we can start developing the app. But first, What’s OpenCV.

OpenCV

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products. As an Apache 2 licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

In the example, we can see how OpenCV and Python make it easy to handle an image since, in OpenCV, an image is represented as a numpy ndarray, so we will be able to apply all operations allowed by numpy.

Note: OpenCV has functions for handling images; this article aims to show how simple it is to work with OpenCV using concepts and other libraries (like numpy) to handle images.

Here, we are going to present the app as a script.

First, we must import cv2.

import cv2

Now, we are going to read an image and crop it.

img = cv2.imread(self.path)
print(type(img), img.shape)

If we execute the script, we’ll get the following out.

<class 'numpy.ndarray'> (492, 740, 3)

Here, we can notice that we have loaded an image from a PNG file, and this is represented as a numpy ndarray, with each element being a pixel of the image; we can access the shape of ndarray, which means rows, columns, and channels (if the image is color).

Now, we can start playing with the image as a numpy object. so we can crop the image by doing the following.

crop = img[100:200,100:300]
print(type(crop))

As you can see, we are slicing to crop the original image.

We will resize the images using the OpenCV method for a more visible example.

img = cv2.resize(img, (0,0), fx=1.5, fy=1.5)
crop = cv2.resize(crop, (0,0),fx=2.5, fy=2.5)

Now, we will apply a simple and rustic filter to modify the image, iterating over the numpy ndarray. Our image is a matrix so we can apply an operation to each pixel like a matrix in Python.

# Applying a Filter
rows, cols = crop.shape[:2]
for i in range(rows*cols):
crop[i//cols][i%cols] = crop[i//cols][i%cols]*1.8

Each pixel will be multiplied by 1.8. Again, we can do this because the cropped object is a numpy ndarray.

We must add the following lines to show our original and cropped-modified image.

cv2.imshow("Original-image", img)
cv2.imshow("Crop-image", crop)

cv2.waitKey(0)
cv2.destroyAllWindows()

The method cv2.imshow() creates a window to display the image. cv2.waitWay(0) waits until we press any key; after that, all windows are “destroyed” (cv2.destroyAllWindows()).

Note: here is the script example.py

Summary

This is a very simple example of how to use OpenCV and Python. We can get a more elaborate application to do almost the same here (read_img.py).

I hope you enjoy it.

--

--

Rodrigo Ancavil
Rodrigo Ancavil

Written by Rodrigo Ancavil

IT Architect and Software Engineer

No responses yet