我有以下代码:
from PIL import Image
import numpy as np
a = np.ones((512, 256, 3)).astype(int)*255
image = Image.fromarray(a, "RGB")
image.save("test.png", "PNG")
我希望这会产生尺寸为512x256的白色图像。 相反,我得到了这个:
出于某种原因,这个图像是一系列条形而不是纯色。
我做错了什么?它是numpy数组的格式吗?
答案 0 :(得分:2)
而不是astype(int)
使用astype(np.uint8)
甚至更好
a = np.full((512, 256, 3), 255, dtype=np.uint8)
您看到的条纹是将int值重新解释为uint8
s序列的结果。
答案 1 :(得分:1)
改为使用Image.new
。
image = Image.new('RGB', (512, 256), color=(255, 255, 255))
image.save("test.png", "PNG")