我有一个RGBA元组的列表,我想将其转换为图像并保存

时间:2018-04-11 22:51:53

标签: python image list rgba

我有这些:

pixels = [(255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (204, 204, 204, 255), (204, 204, 204, 255), (119, 119, 119, 255), (119, 119, 119, 255), (204, 204, 204, 255), (204, 204, 204, 255), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0)......]

这是一个包含大量tuple s的列表,我想将其转换为图片。

我无法找到任何解决方案。

2 个答案:

答案 0 :(得分:1)

您可以使用Python Imaging Library作为替代方案,将数组转换为图片:

from PIL import Image

pixels = [(255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (204, 204, 204, 255), (204, 204, 204, 255), (119, 119, 119, 255), (119, 119, 119, 255), (204, 204, 204, 255), (204, 204, 204, 255), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0), (255, 255, 255, 0)......]

#Sample values of width and height. Change them according to your needs.
size = (50,50)

fileName = "image.png"

image = Image("RGBA",size)
image.putdata(pixels)
image.save(fileName)

答案 1 :(得分:0)

PyPNG。将NumPy数组写入图像。

png.from_array([[255, 0, 0, 255],
                [0, 255, 255, 0]], 'L').save("file.png")

会这样做