What are the multiple ways to open an image in Python?
There are different problems where we need to manipulate, process, and observe images. Python provides multiple ways to open images.
Following are some of those methods:
OpenCV
These are the image-related functions in the OpenCV library of Python:
1.cv2.imread()2.cv2.imshow()
Example
import cv2image = cv2.imread('lake.jpg')cv2.imshow("OpenCV Image",image)cv2.waitKey(0)
Explanation
Line 1: We import
cv2, OpenCV, in Python.Line 2: We apply the
imread()function. The first parameter is the image name, and the second parameter tells the function to read all colors from the image. It stores the image data into the variable namedimage.Line 3: We display the image with the title
"OpenCV Image".Line 4: The function
waitkey()with parameter0tells the program to open the image until a keystroke.
Pillow
The only library of Python that can be considered image-only. The following are the functions that are used to open an image via pillow:
1.open()2.show()
Example
from PIL import Imageimg = Image.open("lake.jpg")img.show()
Explanation
Line 1: We use the
PILlibrary and make an instance namedImgage.Line 2: We open the image in the
imagevariable using theopen()function and give the image name as the parameter.Line 3: We display the image.
Matplotlib
This package is Python's way of analyzing and displaying data. We can use this package to open images as well by using these functions:
1. .image.imread()2. .pyplot.imshow()
Example
from matplotlib import image as imgfrom matplotlib import pyplot as pltplt.title("lake.jpg")image = img.imread("lake.jpg")plt.imshow(image)plt.show()
Explanation
Line 1: We import the
imagepackage of the matplotlib library asimg.Line 2: We import the
pyplotpackage of the matplotlib library asplt.Line 3: We give a title to the image.
Line 4: We read the image into the
imagevariable using theimread()function of theimagepackage.Line 5: We show the image to the back-end using the
imshow()function of thepyplotpackage.Line 6: We show the image to the front-end using the
show()function of thepyplotpackage.
TensorFlow
It is a machine learning package of Python. Matplotlib is used in conjunction with this package to work on images. The following functions are used:
1. io.read_file()2. image.decode_png()
Example
from tensorflow import iofrom tensorflow import image as imgfrom matplotlib import pyplot as plttensor_img = io.read_file("lake.jpg")tensor_img = img.decode_png(tensor_img, channels=3)plt.imshow(tensor_img)
Explanation
Line 1–3: We import the
tensorflowandmatplotlibpackages.Line 4: We read the image into
tensor_imgvariable using theread_file()function of theiopackage oftensorflow. Its parameter is the image name.Line 5: We decode the image data in the
tensor_imgvariable using thedecode_png()function of theimagepackage oftensorflow. The first parameter is the variable containing image data, and thechannelis used for setting usage; the default value is 3.GPU Graphics processing unit Line 6: We show the image using the
imshow()function of theplyplotpackage.
Scikit-Image
It is another Python library used for analytics. The following functions of this library can be used to open an image:
1. io.imread()2. io.imshow()
Example
from skimage import ioimage = io.imread("lake.jpg")io.imshow(image)
Explanation
Line 1: We import the
iopackage fromskimagelibrary.Line 2: We store the image data into the
imagevariable via theimread()function of theiopackage. Its parameter is the image name.Line 3: We display the image using the
imshow()function of theiopackage.
Conclusion
We can open images in Python using the methods mentioned above for data processing, image processing, and statistical/analytical procedures.
Free Resources