在PIL中,为什么不转换('L')转动图像灰度?

1 python numpy python-imaging-library

对于我正在编写的程序,我需要将RGB图像转换为灰度,并使用PIL将其作为NumPy数组读取.

但是,当我运行以下代码时,它会将图像转换为灰度,但会转换为奇怪的颜色失真,有点像热像仪的输出,如图所示.

知道问题可能是什么?

谢谢!

http://www.loadthegame.com/wp-content/uploads/2014/09/thermal-camera.png

from PIL import Image
from numpy import *
from pylab import *

im = array(Image.open('happygoat.jpg').convert("L"))

inverted = Image.fromarray(im)

imshow(inverted)
show()
Run Code Online (Sandbox Code Playgroud)

orl*_*rlp 6

matplotlib imshow旨在科学地表示数据 - 而不仅仅是图像数据.默认情况下,它配置为使用高对比度调色板.

您可以通过传递以下选项强制它使用灰度显示数据:

import matplotlib.cm
imshow(inverted, cmap=matplotlib.cm.Greys_r)
Run Code Online (Sandbox Code Playgroud)