【问题标题】:Why loading a 2d array with PIL.Image.fromarray mode="L" change the image?为什么用 PIL.Image.fromarray mode="L" 加载二维数组会改变图像?
【发布时间】:2021-11-29 01:15:37
【问题描述】:

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

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 图像模式 here
  • L = 8位像素,黑白..所有值都是0-255..为什么图像不一样?

标签: python image numpy python-imaging-library


【解决方案1】:

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

【讨论】:

  • 谢谢!是这样的
猜你喜欢
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2020-07-24
  • 2015-08-31
相关资源
最近更新 更多