user9703439
user9703439

Reputation:

Strange image produced by matplotlib

I have a png image as follows: enter image description here

I wrote the following script to read into matplotlib.

import numpy as np, matplotlib.pyplot as plt
from PIL import Image

fi = "map.png"
data = np.array(Image.open(fi))
print data.shape

plt.imshow(data)
plt.show()

But the image looks different.

enter image description here How to make it look similiar to the first one? I mean, the colors.

Upvotes: 1

Views: 300

Answers (2)

user_D_A__
user_D_A__

Reputation: 460

The image you have provided is not a Grayscale image

    import matplotlib.pyplot as plot
    import matplotlib.image as image_rgb
    image = image_rgb.imread("image.png")
    plot.imshow(image)
    plot.show()

I have not used the PIL library and it produces apt results.

Upvotes: 0

xnx
xnx

Reputation: 25518

Matplotlib handles images much more transparently if you use the mpimg package:

import numpy as np, matplotlib.pyplot as plt
import matplotlib.image as mpimg

fi = "map.png"
data = mpimg.imread(fi)
print(data.shape)
plt.imshow(data)
plt.show()

enter image description here

Upvotes: 1

Related Questions