PIL 转换后无法使用 matplotlib 绘制图像('L')
Can't plot image with matplotlib after PIL convert('L')
我尝试将 RGB 图像转换为灰度图像并绘制它们。但是我无法在 PIL convert('L') 之后绘制灰度图像。 Spyder 控制台中报告了以下错误 (Python3.6)。但是,如果我不使用 convert('L').
可以绘制原始图像
文件 "d:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py",第 430 行,在 _make_image 中
如果 A.mask.shape == A.shape:
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
尝试使用 LA
而不是 L
,如下所示:
from PIL import Image, ImageFilter
import matplotlib.pyplot as plt
file_name = r'images.4.1.png'
im0 = Image.open(file_name).convert('LA')
plt.imshow(im0, cmap='gray')
plt.show()
该参数指定转换模式,在您的情况下,PNG 文件可能有一个 alpha 通道,因此需要 A
。
另请参阅:Pillow modes
我尝试将 RGB 图像转换为灰度图像并绘制它们。但是我无法在 PIL convert('L') 之后绘制灰度图像。 Spyder 控制台中报告了以下错误 (Python3.6)。但是,如果我不使用 convert('L').
可以绘制原始图像文件 "d:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py",第 430 行,在 _make_image 中 如果 A.mask.shape == A.shape:
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
尝试使用 LA
而不是 L
,如下所示:
from PIL import Image, ImageFilter
import matplotlib.pyplot as plt
file_name = r'images.4.1.png'
im0 = Image.open(file_name).convert('LA')
plt.imshow(im0, cmap='gray')
plt.show()
该参数指定转换模式,在您的情况下,PNG 文件可能有一个 alpha 通道,因此需要 A
。
另请参阅:Pillow modes