如何将ndarray转换为IMAGE类型

时间:2018-08-13 13:48:07

标签: python python-imaging-library

load_image函数不接受ndarray类型。该功能仅接受IMAGE类型。

是否可以将ndarray类型转换为IMAGE类型或_ImageCrop类型转换为IMAGE类型?

enter image description here

2 个答案:

答案 0 :(得分:1)

据我对问题的理解,您想将numpy数组转换为图像格式(PIL)。可以通过以下代码完成:

import numpy
import PIL

#Convert nparray to PIL image
img = PIL.Image.fromarray(arr)    #arr is numpy array

答案 1 :(得分:0)

import numpy as np
from PIL import Image

# monochrome:
X1 = np.random.randint(0, 255, size=(256,256), dtype=np.uint8)
image1 = Image.fromarray(X1, "L")
image1.show()

# color:
X2 = np.random.randint(0, 255, size=(256,256,3), dtype=np.uint8)
image2 = Image.fromarray(X2, "RGB")
image2.show()

查看不同的模式:
https://pillow.readthedocs.io/en/5.2.x/handbook/concepts.html#concept-modes