melatonin15
melatonin15

Reputation: 2289

PIL image and matplotlib plot gets saturated black and white for png image

I have the following mammogram image and I am trying to read this using PIL image and then plot it using matplotlib:

enter image description here

The code I am using is:

%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)

But I am getting this image:

enter image description here

Why it's not showing the correct image?

Upvotes: 4

Views: 2965

Answers (1)

Serenity
Serenity

Reputation: 36695

You have to convert image after loading to numpy array to process with matplotlib. To show image in grey scale use grey colormap over-vise image will be shown in color mode.

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

Upvotes: 7

Related Questions