Python 读取图片

1. matplotlib.image as mpimg读取图片,读取出来的格式为numpy.ndarray

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

I = mpimg.imread('ori.jpg')
plt.imshow(I)
plt.show()     #这一步是必须的,不然显示不出来

2. cv2读取图像

查看opencv版本,cv2读取的图片格式为BGR,plt显示的格式为RGB,因此需要格式转换

import cv2
print (cv2.__version__)
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = cv2.imread('ori.jpg')
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()

3. pillow显示图片

from PIL import Image
img=Image.open('d:/dog.png')
img.show()

4. 图片的保存

img.save('save.jpg')

 

 

你可能感兴趣的:(Python)