PIL图像和matplotlib绘图在处理PNG图像时出现黑白过度饱和的问题

4

我有以下乳腺X光图像,我正在尝试使用PIL图像读取它,然后使用matplotlib绘制它:

enter image description here

我使用的代码是:

%matplotlib inline
from PIL import Image
from matplotlib.pyplot import imshow
from scipy.misc import imread
path = './sample.png'
image = Image.open(path).convert('RGB')
imshow(image)

但是我得到了这张图片:

enter image description here

为什么它没有显示正确的图像?
1个回答

6

在加载图像后,您需要将其转换为numpy数组才能使用matplotlib进行处理。要以灰度显示图像,请使用grey色彩映射,过度显示的图像将以彩色模式显示。

import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
from matplotlib.pyplot import imshow
from scipy.misc import imread
path = './1.png'
image = Image.open(path)
plt.imshow(np.asarray(image), cmap='gray')
plt.show()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接