Protoss Reed
Protoss Reed

Reputation: 265

Python. Matplotlib inverted image

I haven't an idea what is wrong here.

import matplotlib.pyplot as plt

im = plt.imshow(plt.imread('tas.png'))
plt.show()

And the Y axis has inverted.
So I wrote an argument origin='lower'.

im = plt.imshow(plt.imread('tas.png'), origin='lower')
plt.show()

And what I have got.
The Y axis came normally, but image now has been inverted.

Also when i try to re-scale axis X and Y, the image hasn't got smaller, only cut out piece.

Thank you in advance. I would be so grateful for some help.

Upvotes: 10

Views: 11028

Answers (1)

tacaswell
tacaswell

Reputation: 87566

You are running into an artifact of how images are encoded. For historical reasons, the origin of an image is the top left (just like the indexing on 2D array ... imagine just printing out an array, the first row of your array is the first row of your image, and so on.)

Using origin=lower effectively flips your image (which is useful if you are going to be plotting stuff on top of the image). If you want to flip the image to be 'right-side-up' and have the origin of the bottom, you need to flip your image before calling imshow

 import numpy as np
 im = plt.imshow(np.flipud(plt.imread('tas.png')), origin='lower')
 plt.show()

Upvotes: 21

Related Questions