Python | 读取和保存图片的几种方式

Image

from PIL import Image
img = Image.open('img.jpg')
print(type(img))  # <class 'PIL.JpegImagePlugin.JpegImageFile'>
print(img)  # <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=742x531 at 0x14E818079E8>
img.show()
img.save('img_copy.jpg')
import numpy as np
img = np.array(img)
print(type(img))  # <class 'numpy.ndarray'>
print(img.shape)  # (531, 742, 3)

skimage

from skimage import io
img=io.imread('img.jpg')
print(type(img))  # <class 'imageio.core.util.Array'>
print(img.shape)  # (531, 742, 3)
io.imshow(img)
io.show()
io.imsave('img_copy.jpg', img)

matplotlib

import matplotlib.pyplot as plt
img = plt.imread('img.jpg')
print(type(img))  # <class 'numpy.ndarray'>
print(img.shape)  # (531, 742, 3)
plt.imshow(img)
plt.show()
plt.imsave('img_copy.jpg', img)

cv2

import cv2
img = cv2.imread('img.jpg')
print(type(img))  # <class 'numpy.ndarray'>
print(img.shape)  # (531, 742, 3)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.imwrite('img_copy.jpg', img)

scipy

import scipy.misc
img = scipy.misc.imread('img.jpg')
print(type(img))  # <class 'numpy.ndarray'>
print(img.shape)  # (531, 742, 3)
from os import environ
environ['SCIPY_PIL_IMAGE_VIEWER'] = ''  # 这里填 image viewer 路径
scipy.misc.imshow(img)
scipy.misc.imsave('img_copy.jpg', img)

posted on 2020-12-30 13:54  cdekelon  阅读(242)  评论(0编辑  收藏  举报

导航