Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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']:

@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

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