如何修复 PIL 模块 python 中的此错误 "fromarray" 函数?
How to fix this error "fromarray" function from PIL module python?
我只是想学习如何使用 replit.com
从数组创建图像
import numpy as np
from PIL import Image
emtpy=np.zeros((24,24))
emtpy[:,:]=[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]
print(emtpy)
img=Image.fromarray(emtpy, "1")
img.save("test.png")
这是我得到的:
(它是 24x24 矩阵)
和图像:
为什么他们看起来不像数组?
我已经尝试了另一种 fromarray 模式,将“1”更改为“L”等,它们仍然不起作用
(“L”模式)
我的期望是:
如果您查看数据,您会发现它是 float
,因为它有一个小数点。
您需要一个无符号的 8 位整数:
emtpy=np.zeros((24,24), dtype=np.uint8)
然后,当您创建一个 Image
对象时,您需要执行此操作以使其处于 8 位像素的正确范围内,即 0..255:
img=Image.fromarray(emtpy*255, "L")
我只是想学习如何使用 replit.com
从数组创建图像import numpy as np
from PIL import Image
emtpy=np.zeros((24,24))
emtpy[:,:]=[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]
print(emtpy)
img=Image.fromarray(emtpy, "1")
img.save("test.png")
这是我得到的:
和图像:
为什么他们看起来不像数组?
我已经尝试了另一种 fromarray 模式,将“1”更改为“L”等,它们仍然不起作用
(“L”模式)
我的期望是:
如果您查看数据,您会发现它是 float
,因为它有一个小数点。
您需要一个无符号的 8 位整数:
emtpy=np.zeros((24,24), dtype=np.uint8)
然后,当您创建一个 Image
对象时,您需要执行此操作以使其处于 8 位像素的正确范围内,即 0..255:
img=Image.fromarray(emtpy*255, "L")