Python读取图片
Python 读取图片
1. PIL
目前主要使用python3,python2已经不支持更新了。使用anaconda管理python的各种库较为方便。
pip install pillow
PIL 读取的通道顺序为RGB,实际上使用的是PIL库中的Image类
读取shape为(Width, Height)
,省略了通道
from PIL import Image
img=Image.open(imagepath)
img.show()
im.save(outfilepath, format, options…)
2. cv2
cv2是OpenCV的python版本
pip install opencv-python
cv2读取的通道顺序是BGR
cv2读取的数据类型是numpy数组,是uint8的整型数据,范围为0-255,numpy 数组的shape是(Height, Width, Channel)
import cv2
img= cv2.imread(imagepath )
cv2.imshow('title', img)
cv2.waitKey(0) # 按任意键继续
cv2.imwrite(outfilepath)
3. matplotlib.image
安装好了matplotlib其中就包括了matplotlib.image
plt.imread()
读取的通道顺序是RGB
plt.imread()
读取的数据类型是numpy数组,是uint8的整型数据,范围为0-255
numpy 数组的shape是(Height, Width, Channel)
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img = mpimg.imread(imagepath)
plt.imshow(img)
plt.show()