首页 > 解决方案 > 为什么用 PIL.Image.fromarray mode="L" 加载二维数组会改变图像?

问题描述

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

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')

看图片:

在此处输入图像描述

在此处输入图像描述

标签: pythonimagenumpypython-imaging-library

解决方案


PIL 要求L模式图像为 8 位,请参见此处。所以,如果你传入你的 16 位图像,其中每个高字节都是零,那么每个第二个像素都是黑色的。


推荐阅读