Analytics Vidhya
Published in

Analytics Vidhya

Python Tip #7: OpenCV — CV2: imread() and resize() | Dev Skrol

Photo by Hari Nandakumar on Unsplash

In this article we are going to learn some of the major use of OpenCV or CV2.

OpenCV is a library mainly used to read/write and manipulate images.

You can find the official page here https://pypi.org/project/opencv-python/.

Installation: pip install opencv-python

Importing Library: import cv2

Supported Versions: 3.6, 3.7, 3.8, 3.9

Basic understanding of image:

We all know that an image is nothing but an array of pixels.
Each pixel is a number.

Based on the number the color of the pixel will be.

Example: 0 — Black and 1 — White.

Read Image: cv2.imread()

Lets consider we have a small image of 28 X 28 pixels.

import cv2 
##Read image
image_path="4.png"
img=cv2.imread(image_path,0)
#Now we have the image in img variable.
#To visualize the image we can use matplotlib.pyplot
import matplotlib.pyplot as plt
plt.imshow(img, cmap="gray")

From the above program, you can see that we have a image “4.png”.
cv2.imread(filename, 0) — Read and returns the image. 0 mentions the color of the image. 0 as second parameter will read the image in gray-scale mode. i.e. the image will only be a 2D array and the 3rd dimension for color will not be added.

This image can be visualized using matplotlib.pyplot imshow(img). If we do not mention the cmap value, matplotlib will automatically assign a colormap to it.

The shape of this image can be found using the shape attribute as it is an array of pixels(again numbers).

Lets explore the values of the array.

The array shape is 2D 28 X 28. To have a good view of array in notebook result, I have resized the image to 18 X 18.

import numpy as np 
number_arr = np.asarray(img)
img_resized = cv2.resize(img, (18,18))
print(np.asarray(img_resized))

You can see that there is a 4 shape in the array where numbers are > 0.

Interesting! isn’t it? Enjoy!

We will meet with a new tip in Python. Thank you! 👍

Like to support? Just click the heart icon ❤️.

Happy Programming!🎈.

You may like:

Digital Image Processing — Morphological Operations (Erosion & Dilation) explained in detail with OpenCV

A Tip A Day — Python Tip #8: Why should we Normalize image pixel values or divide by 255?

Originally published at https://devskrol.com on February 11, 2021.

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

Make your Ubuntu Server VPS more secure against unauthorized access

Asset Protection Law

Python Script to Generator and Read QR Code

How To Create Diversity While Testing — “Group Testing”

Getting Started with DynamoDB: A Beginner’s Guide

5 Lessons learned while making my first open-source contribution

Farming 2.0 Audit is complete!

Adobe Experience Manager: How to start working as an AEM author and Quality Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Asha Ponraj

Asha Ponraj

Data Science & Machine Learning Enthusiast | Software Developer | Blogger | https://devskrol.com/ | www.linkedin.com/in/asha-ponraj-a5a76b50

More from Medium

Single row functions available in Oracle and MySQL

#2 How to find the language school in the US?

Aware

drawing of a lady floating with arms spread. Her body is made of cosmic stars.