2015年6月28日

Matplotlab显示灰度图问题

我想要使用Matplotlab显示图片转换后的灰度图。代码如下:

from PIL import Image
from pylab import *

im = array(Image.open('tiger.jpg').convert('L'))
imshow(im)
show()

与书上的例子几乎一模一样,运行结果却是这样:

很明显,以上并不是灰度图。出了什么问题呢?然后我看到了这个StackOverFlow上的问题。以下是原因所在:

matplotlib’s imshow is aimed at scientific representation of data – not just image data. By default it’s configured to use a high constrast color palette.

于是修改代码如下:

from PIL import Image
from pylab import *
import matplotlib.cm

im = array(Image.open('tiger.jpg').convert('L'))
imshow(im, cmap=matplotlib.cm.Greys_r)
show()

再次运行,得到结果:

——————————————————————————————————————-

另一种解决方法:在显示图像之前,调用pylab.gray()函数。


“以书为舟,遨游尘世”,
最好的免费 kindle 电子书分享站:

You may also like...

发表回复

您的电子邮箱地址不会被公开。


*