Python: Basic Image processing techniques using open-cv

What is opencv

Python: Basic Image processing techniques using open-cv
Photo by Alvin Lenin on Unsplash
Join Medium with my referral link - Konstantinos Patronas
Read every story from Konstantinos Patronas (and thousands of other writers on Medium). Your membership fee directly…

What is opencv

OpenCV-Python is a library of Python bindings designed to solve computer vision problems, apart from computer vision can be also used for image processing.

How to install opencv

Using pip we can install opencvpip install opencv-python

How to import opencv to our python script

The import statement loads libraries, cv2 is the opencv library name of opencv in python.import cv2

How to read an image with opencv

Assuming we have an image file named cat.jpg, to read the image use the following.img = cv2.imread("cat.jpg")

How to get image properties

After reading the image we can extract its properties in a tuple structure. The first element is the width, second is the height and third the number of channels (Note that opencv does not use RGB, but instead uses BGR).>>> print(img.shape)
(534, 799, 3)

How to convert from BRG to RGB color format

If we want to display the image in other libraries like matplotlib which reads the images in RGB format we need to convert the image.

RGB_img =

How to resize image by ratio

We need first to set the ratio, we want to resize it by 50% in this example>>> ratio_resize = 50
>>> width  = int(img.shape[1] * ratio_resize / 100)
>>> height = int(img.shape[0] * ratio_resize / 100)
>>> new_size = (width,height)
>>> img_resized = cv2.resize(img,new_size)
>>> print(img_resized.shape)
(267, 399, 3)

How to resize image horizontally

In this case we will give to new_width the value we want, the interpolation property calculates pixel values for the new image from the original one.>>> new_width = 100
>>> new_size = (new_width,img.shape[0])
>>> img_resized = cv2.resize(img,new_size,interpolation=cv2.INTER_AREA)
>>> print(img_resized.shape)
(534, 100, 3)

How to resize image vertically

Using an almost similar technique we resize an image vertically.>>> new_height = 100
>>> new_size = (img.shape[1],new_height)
>>> img_resized = cv2.resize(img,new_size,interpolation=cv2.INTER_AREA)
>>> print(img_resized.shape)
(100, 799, 3)

How to rotate image 90 degrees clockwise

Rotating an image is super easy!>>> img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
>>> print(img.shape)
(799, 534, 3)

How to rotate image 90 degrees counter-clockwise>>> img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
>>> print(img.shape)
(534, 799, 3)

How to rotate an image horizontally>>> img = cv2.flip(img, 1)

How to rotate an image vertically>>> img = cv2.flip(img, 0)

How to convert image to grayscale

gray_image =

How to save image

Also saving images is very easy!

cv2.imwrite('filename.jpg',img)

I hope you found this article easy to read and inspired you to use opencv in future projects!

Join Medium with my referral link - Konstantinos Patronas
Read every story from Konstantinos Patronas (and thousands of other writers on Medium). Your membership fee directly…