为什么用 PIL.Image.fromarray mode="L" 加载二维数组会改变图像?

Why loading a 2d array with PIL.Image.fromarray mode="L" change the image?

为什么这段代码绘制不同的图像?

from PIL import Image
import numpy as np

x = (np.random.random((32,32))*255).astype(np.int16)

img1 = Image.fromarray(x, mode='L')
img2 = Image.fromarray(x)

plt.imshow(img1, cmap='gray')
plt.imshow(img2, cmap='gray')

查看图片:

PIL 要求 L 模式图像为 8 位,请参阅 here。因此,如果您传入 16 位图像,其中每个高字节为零,则每隔一个像素将为黑色。