python - PIL 图像和 matplotlib 图为 png 图像获取饱和黑白

标签 python image matplotlib python-imaging-library

我有以下乳房 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

为什么它没有显示正确的图像?

最佳答案

您必须在加载到 numpy 数组后转换图像才能使用 matplotlib 进行处理。要以灰度显示图像,请使用 grey colormap over-vise 图像将以彩色模式显示。

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

关于python - PIL 图像和 matplotlib 图为 png 图像获取饱和黑白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46785782/

相关文章:

python - 如何随着时间的推移绘制信号的积分?

python - 在 Mac 上将 Django 安装到 Python 3.3

数组中的 JavaScript 图像

python - 向 matplotlib/seaborn 图中的某些单元格添加自定义边框

ios - 如何去除图像的噪点?

ios - 我可以使用 jpg 而不是 png 作为 iOS 网络应用启动图像吗?

python - 将图例添加到散点图

python - 如何在 Keras 中创建自定义目标函数?

python - 在Python中,如何进行字符串替换并检索替换的子字符串?

python - While 循环列表?