There are several options to load image data in Python, provided by:
1. OpenCV
2. matplotlib
3. PIL
1. in OpenCV:
import cv2
import numpy as np
img = cv2.imread('examples.png') # 默认是读入为彩色图,即使原图是灰度图也会复制成三个相同的通道变成彩色图
img_gray = cv2.imread('examples.png',0) # 第二个参数为0的时候读入为灰度图,即使原图是彩色图也会转成灰度图
print(type(img), img.dtype, np.min(img), np.max(img))
print(img.shape)
print(img_gray.shape) # opencv读进来的是numpy array,类型是uint8
2. in matplotlib:
import matplotlib.pylab as plt
import matplotlib.image as mpimg
img=plt.imread("xxx.png") #numpy array
or
img=mpimg.imread("xxx.png") #all the data loaded is in float32 format numpy array
3. in PIL:
from PIL import Image
img=Image.open("xxx.png") #an image object, not numpy array, need to implement the following to convert:
img=np.array(img) or
img=np.asarray(img)
网友评论