In this first part of the image processing series, we’ll talk about loading images in Python. We’ve all been through a lot of changes in the age of the technical revolution, and a large part of it was the growth in data.
As data has grown substantially in the past few years, methods to implement it have also grown.
Ranging all the way from Data in computers to Data in wristwatches, Data is collected in all sort of formats now.
Interestingly enough, one such method in collecting data comes from Images.
After research on trying to enhance images back in the 1950s, Bell Labs found that there was to a be a new branch of study.
What is Digital Image Processing?
The main principle behind Digital Image Processing was to enhance the quality of existing images back in the day.
Now, we have better cameras and better methods to capture data.
However, Digital Image Processing has also come a long way in the same regard.
We’ve come from trying to enhance images, to retrieving information from images, to training models that can predict patterns in images.
Data has come a long way and Image Processing has not fallen behind in any way.
So, let us begin exploring Image Processing and work with it using the
What is Python OpenCV?
OpenCV stands for Open Computer Vision and is considered to be one of the most popular Image Processing modules that exist.
OpenCV is used for a large number of tasks with respect to images, and we’ll be using it throughout the series.
The module focuses on various aspects such as Image Processing, Feature Description and Identification, and Object Detection.
In order to install the OpenCV module, you’ll need to use the pip package manager as follows:
pip install opencv-python
Steps for Loading Images in Python
For this article, we’ll be starting slow, and focusing on how to import the module, reading, and writing the image. We’ll also try to read the file location using the
Importing the modules into the script requires the following commands,
# importing argument parsers import argparse #importing the OpenCV module import cv2
There are two methods to initializing objects, as we know, one is through user input and the other strict input.
So, here are the methods to do both!
1. User Input for the Image Location
# initializing an argument parser object ap = argparse.ArgumentParser() # adding the argument, providing the user an option # to input the path of the image ap.add_argument("-i", "--image", required=True, help="Path to the image") # parsing the argument args = vars(ap.parse_args()) # reading the image location through args # and reading the image using cv2.imread image = cv2.imread(args["image"])
2. Strict Manual Input
# Reading the image location using a hardcoded path # and using cv2.imread to read the image image = cv2.imread( "C:\\Users\\tester\\PycharmProjects\\image-processing\\assets\\ey.jpg" )
As we’ve read the image into a variable using the
cv2.imread function, we can proceed to invoke the elements of the image.
Here, we are displaying the shape or the dimensions of the image, along with the number of channels that it contains.
We then use the
cv2.imshow() function to display the image.
cv2.waitkey() function waits for the user to input a keystroke to close the display window.
# printing out the various dimensions of the image print("width : ", image.shape) print("height : ", image.shape) print("channels : ", image.shape) # Displays the image using cv2.imshow cv2.imshow("Image", image) # Displays the image until the user presses a key closing the display cv2.waitKey(0)
Writing the image to a file is very easy using the
cv2 module, with a simple function called
All you have to do is provide the name of the image, and you’re good to go!
# Writing the image into the same folder as the script # But under a new name cv2.imwrite("newimage.jpg", image)
Upon executing the code, you should receive an output, notifying you of the image dimensions.
width: 600 height: 600 channels: 3
Followed by another file showing up in the same directory, with the name that you’ve given to it.
There will be more articles regarding the functionality of the
cv2 module and this was just the start as mentioned in the title.
So, look forward to editing images rather than just loading them up and saving them in another file in the future!
In case you want to jump into the future and look at some advanced use cases of OpenCV, you might want to look into OpenCV and Facial Recognition, and a Java implementation of Android and CameraX OpenCV.