Python PIL bitmap/png 来自模式为 1 的数组

Python PIL bitmap/png from array with mode=1

有史以来第一次使用 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

在 numpy 数组中使用模式 1 时似乎存在问题。作为解决方法,您可以使用模式 L 并在保存之前转换为模式 1。下面的代码片段生成了预期的棋盘。

from PIL import Image
import numpy as np

if __name__ == '__main__':
    g = np.asarray(dtype=np.dtype('uint8'), a=[
        [0, 255, 0, 255, 0, 255, 0, 255],
        [255, 0, 255, 0, 255, 0, 255, 0],
        [0, 255, 0, 255, 0, 255, 0, 255],
        [255, 0, 255, 0, 255, 0, 255, 0],
        [0, 255, 0, 255, 0, 255, 0, 255],
        [255, 0, 255, 0, 255, 0, 255, 0],
        [0, 255, 0, 255, 0, 255, 0, 255],
        [255, 0, 255, 0, 255, 0, 255, 0]
    ])
    print(g)

    i = Image.fromarray(g, mode='L').convert('1')
    i.save('checker.png')

我认为这是一个错误。已经反馈了on Github. Although some fix has been commited,好像没有解决这个问题。如果您使用模式 "L" 然后将图像转换为模式“1”,则一切正常,因此您可以将其用作解决问题的方法:

from PIL import Image
import numpy as np

if __name__ == '__main__':
    g = np.asarray(dtype=np.dtype('uint8'), a=[
        [0, 255, 0, 255, 0, 255, 0, 255, ],
        [255, 0, 255, 0, 255, 0, 255, 0, ],
        [0, 255, 0, 255, 0, 255, 0, 255, ],
        [255, 0, 255, 0, 255, 0, 255, 0, ],
        [0, 255, 0, 255, 0, 255, 0, 255, ],
        [255, 0, 255, 0, 255, 0, 255, 0, ],
        [0, 255, 0, 255, 0, 255, 0, 255, ],
        [255, 0, 255, 0, 255, 0, 255, 0, ],
    ])
    print(g)

    i = Image.fromarray(g, mode='L').convert('1')
    i.save('checker.png')

正如另一个答案所指出的,您 运行 陷入了 Pillow bug,接受的答案很好。

作为 PIL/Pillow 的替代方法,您可以使用 pypng, or you could use numpngw, a library that I wrote to write NumPy arrays to PNG and animated PNG files. It is on github: https://github.com/WarrenWeckesser/numpngw (It has all the boilerplate files of a python package, but the essential file is numpngw.py.) It is also on PyPI.

下面是使用 numpngw.write_png 创建棋盘图像的示例。这将创建一个位深度为 1:

的图像
In [10]: g
Out[10]: 
array([[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],
       [0, 1, 0, 1, 0, 1, 0, 1]], dtype=uint8)

In [11]: import numpngw

In [12]: numpngw.write_png('checkerboard.png', g, bitdepth=1)

这是它创建的图像:

只需使用PyPNG:

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)

    import png
    i = png.from_array(g, mode='L;1')
    i.save('checker.png')