preface
In image processing, it is often used to read and display images, so this article summarizes three ways to read and display images in Python, respectively based on OpencV, Matplotlib, PIL library implementation, and gives example codes, introduced as follows.
OpenCV
OpenCV is a cross-platform computer vision and machine learning software library distributed under the BSD license (open source) that runs on Linux, Windows, Android, and Mac OS operating systems. It is lightweight and efficient — it consists of a series of C functions and a small number of C++ classes. It also provides interfaces to Python, Ruby, MATLAB and other languages and implements many common algorithms in image processing and computer vision.
Import matplotlib.pyplot as PLT # PLT import cv2 # opencV \[B,G,R\] Img1 = cv2.imread('./1.png')\[:, :, (2, 1) 0) # \] read and code in the same directory 1. PNG img2 = cv2. Imread ('. / 1. PNG ') \ [:, :, (2, 1, ['font. Sans-serif '\] = \['SimHei'] # plt.subplot(121) # imshow() Plt.imshow (img1) plt.title(' img1 ') # Do not display coordinate axes plt.axis('off') # Plt.title (' image 2') plt.axis('off') # # Set the default spacing of the subimage plt.tight\_layout() # Show the image plt.show()Copy the code
Matplotlib
Matplotlib is a drawing library for Python. It can be used in conjunction with NumPy and provides an effective open source alternative to MatLab. It can also be used with graphical toolkits such as PyQt and wxPython.
Import matplotlib.pyplot as PLT # PLT import matplotlib.image as mpimg # mpimg img1 = PNG img2 = mpim.imread ('./2.png', 0) Plt.rcparams \['font. Sans-serif '\] = \['SimHei'\] # 中文 数 字 plt.subplot(121) # imshow() Plt.imshow (img1) plt.title(' img1 ') # Do not display coordinate axes plt.axis('off') # Plt.title (' image 2') plt.axis('off') # # Set the default spacing of the subimage plt.tight\_layout() # Show the image plt.show()Copy the code
PIL
PIL(Python Image Library) is a third party Image processing Library for Python, but due to its powerful features and large number of users, it is almost considered the official Image processing Library of Python.
PIL can do many things related to image processing:
-
Image Archives
-
Image Display
-
Image Processing
Import matplotlib.pyplot as PLT # PLT from PIL import Image img1 = image.open ('./1.png') img2 = ['font. Sans-serif '\] = \['SimHei'\] # 文 码 plt.subplot(121) plt.imshow(img1) Subplot (122) plt.imshow(img2) plt.title(' image 2') plt.axis('off') # # Set the default spacing plt.tight\_layout() # show the image plt.show()Copy the code