简体   繁体   中英

pillow: why does mode “1” give a weird image?

inspired by this post (plot an image from an array with pillow.)

I tried this

diamond = np.array([[  0,   0,   0,   0, 255,   0,   0,   0,   0],
       [  0,   0,   0, 255,   0, 255,   0,   0,   0],
       [  0,   0, 255,   0,   0,   0, 255,   0,   0],
       [  0, 255,   0,   0,   0,   0,   0, 255,   0],
       [255,   0,   0,   0,   0,   0,   0,   0, 255],
       [  0, 255,   0,   0,   0,   0,   0, 255,   0],
       [  0,   0, 255,   0,   0,   0, 255,   0,   0],
       [  0,   0,   0, 255,   0, 255,   0,   0,   0],
       [  0,   0,   0,   0, 255,   0,   0,   0,   0]], dtype=uint8)

size = 36
my_dpi = mpl.rcParams['figure.dpi']
plt.subplots(figsize=(size/my_dpi, size/my_dpi))
plt.axis('off')
img = Image.fromarray(diamond, 'L')
img.resize((size,size)).save('diamond.ppm')
plt.imshow(img)

so far, everything work well.

per doc "1" represents (1-bit pixels, black and white, stored with one pixel per byte)

using mode "1" to replace "L" outputs this image

在此处输入图片说明

the expected output should look like

在此处输入图片说明

why is that?

convert to mode 1 is a workaround

size = 36
my_dpi = mpl.rcParams['figure.dpi']
plt.subplots(figsize=(size/my_dpi, size/my_dpi))
plt.axis('off')
img = Image.fromarray(diamond, 'L').convert('1')
img.resize((size,size)).save('diamond.ppm')
plt.imshow(img)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM