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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/image.py
Expand Up @@ -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 == 'lower' else self._A,
bytes=True, norm=True)
_png.write_png(im, fname)

def set_data(self, A):
Expand Down
25 changes: 14 additions & 11 deletions lib/matplotlib/tests/test_image.py
Expand Up @@ -150,21 +150,24 @@ def test_imsave_color_alpha():
# acceptably preserved through a save/read roundtrip.
from numpy import random
random.seed(1)
data = random.rand(16, 16, 4)

buff = io.BytesIO()
plt.imsave(buff, data)
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

data = random.rand(16, 16, 4)
buff = io.BytesIO()
plt.imsave(buff, data, origin=origin)

buff.seek(0)
arr_buf = plt.imread(buff)
buff.seek(0)
arr_buf = plt.imread(buff)

# Recreate the float -> uint8 conversion of the data
# We can only expect to be the same with 8 bits of precision,
# since that's what the PNG file used.
data = (255*data).astype('uint8')
arr_buf = (255*arr_buf).astype('uint8')
# Recreate the float -> uint8 conversion of the data
# We can only expect to be the same with 8 bits of precision,
# since that's what the PNG file used.
data = (255*data).astype('uint8')
if origin == 'lower':
data = data[::-1]
arr_buf = (255*arr_buf).astype('uint8')

assert_array_equal(data, arr_buf)
assert_array_equal(data, arr_buf)

@image_comparison(baseline_images=['image_alpha'], remove_text=True)
def test_image_alpha():
Expand Down