我第一次尝试使用PIL(和numpy)进行操作。我试图通过mode='1'生成一个黑白棋盘图像,但它无效。
from PIL import Image
import numpy as np
if __name__ == '__main__':
g = np.asarray(dtype=np.dtype('uint8'), a=[
[0, 1, 0, 1, 0, 1, 0, 1, ],
[1, 0, 1, 0, 1, 0, 1, 0, ],
[0, 1, 0, 1, 0, 1, 0, 1, ],
[1, 0, 1, 0, 1, 0, 1, 0, ],
[0, 1, 0, 1, 0, 1, 0, 1, ],
[1, 0, 1, 0, 1, 0, 1, 0, ],
[0, 1, 0, 1, 0, 1, 0, 1, ],
[1, 0, 1, 0, 1, 0, 1, 0, ],
])
print(g)
i = Image.fromarray(g, mode='1')
i.save('checker.png')
抱歉,浏览器可能会尝试插入这个内容,但它是一个8x8的PNG图片。
我错过了什么吗?
相关的PIL文档:https://pillow.readthedocs.org/handbook/concepts.html#concept-modes
$ pip freeze
numpy==1.9.2
Pillow==2.9.0
wheel==0.24.0
Image.frombytes(mode='1', size=data.shape[::-1], data=np.packbits(data, axis=1))
比Image.fromarray(data * 255, mode='L').convert('1')
快大约两倍。 - PM 2Ring