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()
Syntax

Example

import cv2
image = 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 named image.

  • Line 3: We display the image with the title "OpenCV Image".

  • Line 4: The function waitkey() with parameter 0 tells 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()
Syntax

Example

from PIL import Image
img = Image.open("lake.jpg")
img.show()

Explanation

  • Line 1: We use the PIL library and make an instance named Imgage.

  • Line 2: We open the image in the image variable using the open() 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()
Syntax

Example

from matplotlib import image as img
from matplotlib import pyplot as plt
plt.title("lake.jpg")
image = img.imread("lake.jpg")
plt.imshow(image)
plt.show()

Explanation

  • Line 1: We import the image package of the matplotlib library as img.

  • Line 2: We import the pyplot package of the matplotlib library as plt.

  • Line 3: We give a title to the image.

  • Line 4: We read the image into the image variable using the imread() function of the image package.

  • Line 5: We show the image to the back-end using the imshow() function of the pyplot package.

  • Line 6: We show the image to the front-end using the show() function of the pyplot package.

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()
Syntax

Example

from tensorflow import io
from tensorflow import image as img
from matplotlib import pyplot as plt
tensor_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 tensorflow and matplotlib packages.

  • Line 4: We read the image into tensor_img variable using the read_file() function of the io package of tensorflow. Its parameter is the image name.

  • Line 5: We decode the image data in the tensor_img variable using the decode_png() function of the image package of tensorflow. The first parameter is the variable containing image data, and the channel is used for setting GPUGraphics processing unit usage; the default value is 3.

  • Line 6: We show the image using the imshow() function of the plyplot package.

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()
Syntax

Example

from skimage import io
image = io.imread("lake.jpg")
io.imshow(image)

Explanation

  • Line 1: We import the io package from skimage library.

  • Line 2: We store the image data into the image variable via the imread() function of the io package. Its parameter is the image name.

  • Line 3: We display the image using the imshow() function of the io package.

Conclusion

We can open images in Python using the methods mentioned above for data processing, image processing, and statistical/analytical procedures.

Copyright ©2024 Educative, Inc. All rights reserved