【图像处理】python常用图像读取和保存方法。(PIL、cv2、scipy 、skimage)

小丫么小阿豪 2022-08-06 05:26:26 阅读数:855

Python处理常用图像

PIL(Python Imaging Library)

import numpy as np
from PIL import Image
# read image
raw_image = Image.open("xxx.jpg")
# image to array
image_array = np.array(image_resize)
# array to image
image_output = Image.fromarray(image_array)
# save image
image_output.save("xxx.jpg")

cv2(opencv)

关于RGB通道色彩问题,可以参考我之前的博文:https://blog.csdn.net/qq_43426078/article/details/123634853

import cv2
# read image, return np.array with BGR
raw_image = cv2.imread("xxx.jpg")
# BGR to RGB
image_rgb = cv2.cvtColor(raw_image,cv2.COLOR_BGR2RGB)
# save image
cv2.imwrite("xxx.jpg", image_resize)

scipy

scipy老版本用的是scipy.misc,新版用的是imageio,遇到问题建议修改代码,不建议降/升scipy版本,防止其他地方出现问题。

from imageio import imread, imsave
# read image
raw_image = imread("xxx.jpg")
# save image
imsave("xxx.jpg", image_resize)

skimage

from skimage import io as skio
# read image
raw_image = skio.imread("xxx.jpg")
# save image
skio.imsave("xxx.jpg", raw_image)
# show image
skio.imshow(raw_image)
版权声明:本文为[小丫么小阿豪]所创,转载请带上原文链接,感谢。 https://blog.csdn.net/qq_43426078/article/details/124024985