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:
These are the image-related functions in the OpenCV library of Python:
1.cv2.imread()2.cv2.imshow()
import cv2image = cv2.imread('lake.jpg')cv2.imshow("OpenCV Image",image)cv2.waitKey(0)
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.
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()
from PIL import Imageimg = Image.open("lake.jpg")img.show()
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.
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()
from matplotlib import image as imgfrom matplotlib import pyplot as pltplt.title("lake.jpg")image = img.imread("lake.jpg")plt.imshow(image)plt.show()
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.
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()
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)
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
Line 6: We show the image using the imshow()
function of the plyplot
package.
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()
from skimage import ioimage = io.imread("lake.jpg")io.imshow(image)
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.
We can open images in Python using the methods mentioned above for data processing, image processing, and statistical/analytical procedures.