PIL转换后无法使用matplotlib绘制图像('L')

问题描述 投票:0回答:1

我试图将RGB图像转换为灰度图像并绘制它们。但是我无法在PIL转换后绘制灰色图像('L')。 Spyder控制台(Python3.6)中报告了以下错误。但是,如果我不使用转换('L'),则可以绘制原始图像。

如果A.mask.shape == A.shape,请在_make_image中输入文件“d:\ ProgramData \ Anaconda3 \ lib \ site-packages \ matplotlib \ image.py”,第430行:

AttributeError:'numpy.ndarray'对象没有属性'mask'

看下面我的python代码:

from PIL import Image, ImageFilter
import tensorflow as tf
import matplotlib.pyplot as plt
file_name='images\\2.4.1.png'
im0 = Image.open(file_name)

plt.imshow(im0)
plt.show()

im = im0.convert('L')
plt.imshow(im, cmap='gray')
plt.show()  # Not working here
python matplotlib python-imaging-library spyder
1个回答
0
投票

尝试使用LA而不仅仅是L如下:

from PIL import Image, ImageFilter
import matplotlib.pyplot as plt

file_name = r'images\2.4.1.png'
im0 = Image.open(file_name).convert('LA')
plt.imshow(im0, cmap='gray')
plt.show()

该参数指定转换模式,在您的情况下,PNG文件可能具有alpha通道,因此需要A

另见:Pillow modes

© www.soinside.com 2019 - 2024. All rights reserved.