Skip to content

Check origin when saving image to PNG - #7731

Merged
NelleV merged 1 commit into
matplotlib:masterfrom
naoyak:writepng-origin-fix
Jan 11, 2017
Merged

NelleV merged 1 commit into
matplotlib:masterfrom
naoyak:writepng-origin-fix

Conversation

@naoyak

@naoyak naoyak commented Jan 3, 2017

Copy link
Copy Markdown
Contributor

Fixes #7656.

Added an origin check and flips the image when appropriate when calling AxesImage.write_png()

Comment thread lib/matplotlib/image.py Outdated
def write_png(self, fname):
"""Write the image to png file with fname"""
if self.origin == 'upper':
self._A = self._A[::-1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not overwrite self._A

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to either flip it in the call to_rgba or flip the return im below. Reassiging _A like this will cause the image to alternate orientation every time you called write_png.

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.0.1 (next bug fix release) Jan 3, 2017
@tacaswell

Copy link
Copy Markdown
Member

Please do not backport this until after 2.0 final is out.

Comment thread lib/matplotlib/image.py Outdated
def write_png(self, fname):
"""Write the image to png file with fname"""
if self.origin == 'upper':
self._A = self._A[::-1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to either flip it in the call to_rgba or flip the return im below. Reassiging _A like this will cause the image to alternate orientation every time you called write_png.

@naoyak
naoyak force-pushed the writepng-origin-fix branch from 2c44b9f to abcdf28 Compare January 3, 2017 22:16
@naoyak

naoyak commented Jan 3, 2017

Copy link
Copy Markdown
Contributor Author

@Kojoley @tacaswell thanks. Is the test failure related to #7726?

@tacaswell

Copy link
Copy Markdown
Member

Yes, thanks, I thought that had been merged!

Comment thread lib/matplotlib/tests/test_image.py Outdated

buff = io.BytesIO()
plt.imsave(buff, data)
plt.imsave(buff, data, origin='lower')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably should save this with both origins and check that you get the correct output in both cases.


buff = io.BytesIO()
plt.imsave(buff, data, origin='lower')
for origin in ['lower', 'upper']:

@naoyak naoyak Jan 4, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell Added test here to test both lower and upper origin settings

@codecov-io

codecov-io commented Jan 4, 2017 •

Copy link
Copy Markdown

Current coverage is 62.11% (diff: 100%)

Merging #7731 into master will decrease coverage by <.01%

@@             master      #7731   diff @@
==========================================
  Files           174        174          
  Lines         56028      56028          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
- Hits          34805      34803     -2   
- Misses        21223      21225     +2   
  Partials          0          0          

Powered by Codecov. Last update 20315e1...d910788

Comment thread lib/matplotlib/image.py Outdated
def write_png(self, fname):
"""Write the image to png file with fname"""
im = self.to_rgba(self._A, bytes=True, norm=True)
im = self.to_rgba(self._A[::-1] if self.origin == 'upper' else self._A,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the wrong polarity?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell ugh, sorry. First I got mixed up on the origin polarity, then got my git rebasing all wrong. I think it should look ok now.

@naoyak
naoyak force-pushed the writepng-origin-fix branch 2 times, most recently from 1ea49c5 to 92391b5 Compare January 4, 2017 06:18
@naoyak

naoyak commented Jan 4, 2017

Copy link
Copy Markdown
Contributor Author

Output:

import numpy as np
from matplotlib.image import imsave

im = np.arange(30*20).reshape((20, 30))
imsave('lower.png', im, origin='lower')
imsave('upper.png', im, origin='upper')

lower
upper

@naoyak
naoyak force-pushed the writepng-origin-fix branch from 92391b5 to d910788 Compare January 4, 2017 07:14
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.0.1 (next bug fix release) Jan 4, 2017
@tacaswell

Copy link
Copy Markdown
Member

Please do not backport this until after 2.0 final.

@NelleV
NelleV merged commit 494dc09 into matplotlib:master Jan 11, 2017
@naoyak
naoyak deleted the writepng-origin-fix branch January 11, 2017 18:29
@QuLogic

QuLogic commented Jan 30, 2017 •

Copy link
Copy Markdown
Member

Backported to v2.0.x as c1a3702.

QuLogic pushed a commit that referenced this pull request Jan 30, 2017
Check origin when saving image to PNG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants