PIL.Image.open在打开PNG文件时仅显示两种颜色

问题描述

我正在尝试使用PIL打开图像(.png)。但是,它仅显示两种颜色的图像。

原始图片(即3.24 MB)。我减小了大小,因为系统仅允许我们上传2MB以下的文件

Original image

当我运行image = Image.open('path/image')时,它显示以下内容

enter image description here

当我运行相同命令但进行转换时,image = Image.open('path/image').convert('L') 我明白了:

enter image description here

这是我绘制图像的方式:

fig,ax = plt.subplots()
ax.imshow(image)
ax.grid(False)

我尝试了数十幅图像。没变化。我强烈猜测由图像通道引起的问题。函数以某种方式将3通道读取为单通道或以其他方式读取。任何想法如何解决???


编辑:这样我就接近了想要的结果。我首先将iamge转换为数组。但这还不够。

enter image description here

代码如下:

image = Image.open(df['Path'][0])
image = np.asarray(image)
plt.imshow(image)

解决方法

最后解决了。将图像转换为数组,并通过cmap读取值。这是完整的代码和结果:

image = Image.open('/path/image')
image = np.asarray(image)
plt.imshow(np.abs(image),cmap = 'gray')

enter image description here