将array转换为image
需要导入的头文件
from PIL import Image
函数
Image.fromarray() # 可以实现将numpy数组的图像数据转换为PIL用的数据对象
实例
pil_img = Image.fromarray(np.uint8(img))
uint8是专门用于存储各种图像的(包括RGB,灰度图像等),范围是从0–255,大于255的值会被截断。
需要注意的是,如果出现read-only错误,并不是转换的错误,一般是你读取的图片的时候,默认选择的是"r","rb"模式有关。
修正的办法: 手动修改图片的读取状态
img.flags.writeable = True # 将数组改为读写模式