Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check origin when saving image to PNG #7731

Merged
merged 1 commit into from Jan 11, 2017

Conversation

naoyak
Copy link
Contributor

@naoyak naoyak commented Jan 3, 2017

Fixes #7656.

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

@@ -521,6 +521,8 @@ def contains(self, mouseevent):

def write_png(self, fname):
"""Write the image to png file with fname"""
if self.origin == 'upper':
self._A = self._A[::-1]
Copy link
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
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
Member

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

@@ -521,6 +521,8 @@ def contains(self, mouseevent):

def write_png(self, fname):
"""Write the image to png file with fname"""
if self.origin == 'upper':
self._A = self._A[::-1]
Copy link
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
Copy link
Contributor Author

naoyak commented Jan 3, 2017

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

@tacaswell
Copy link
Member

Yes, thanks, I thought that had been merged!

@@ -153,7 +153,7 @@ def test_imsave_color_alpha():
data = random.rand(16, 16, 4)

buff = io.BytesIO()
plt.imsave(buff, data)
plt.imsave(buff, data, origin='lower')
Copy link
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']:
Copy link
Contributor Author

@naoyak naoyak Jan 4, 2017

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
Copy link

codecov-io commented Jan 4, 2017

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

@@ -521,7 +521,8 @@ def contains(self, mouseevent):

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
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
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
Copy link
Contributor Author

naoyak commented Jan 4, 2017

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

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.0.1 (next bug fix release) Jan 4, 2017
@tacaswell
Copy link
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
Copy link
Member

QuLogic commented Jan 30, 2017

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.

None yet

6 participants