Home > Article > Backend Development > How to read pictures in Python
Whether it is used for machine learning or deep learning, it requires the operation of reading images.
Method 1: Use the Image function in PIL. This function reads not in array format (recommended learning: Python video tutorial)
At this time you need to use np.asarray(im) or np.array() function
The difference is that np.array() is a deep copy, np.asarray() is a shallow copy Copy
from PIL import Image import numpy as np I = Image.open('./cc_1.png') I.show() I.save('./save.png') I_array = np.array(I) print I_array.shape
Method 2: Use matplotlib.pyplot as plt to display pictures
# matplotlib.image as mpimg 用于读取图片 # 并且读取出来就是array格式 import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np I = mpimg.imread('./cc_1.png') print I.shape plt.imshow(I)
Method 3: Use opencv-python interface
#cv2.imread()读出来同样是array形式,但是如果是单通道的图,读出来的是三通道的 import cv2 I = cv2.imread('./cc_1.png') print I.shape
Method 4: To access images, I generally like to use the library scipy. It reads it in matrix form and saves it in the form of (H, W, C)
import matplotlib.pyplot as plt from scipy import misc import scipy I = misc.imread('./cc_1.png') scipy.misc.imsave('./save1.png', I) plt.imshow(I) plt.show()
Method Five: Use the skimage library
from skimage import io,data img=data.lena() io.imshow(img)
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to read pictures in Python. For more information, please follow other related articles on the PHP Chinese website!