Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 501
hi15 Avatar asked Sep 07 '25 12:09

hi15


1 Answers

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

like image 114
Serenity Avatar answered Sep 10 '25 04:09

Serenity