Playing with PIL (and numpy) for the first time ever. I was trying to generate a black and white checkerboard image through mode=\'1\', but it doesn\'t work.
There seem to be issues when using mode 1
with numpy arrays. As a workaround you could use mode L
and convert to mode 1
before saving. The below snippet produces the expected checkerboard.
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')