Skip to content

Commit

Permalink
Merge pull request #7731 from naoyak/writepng-origin-fix
Browse files Browse the repository at this point in the history
Check origin when saving image to PNG
  • Loading branch information
NelleV committed Jan 11, 2017
2 parents 9b1ece0 + d910788 commit 494dc09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
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']:
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

0 comments on commit 494dc09

Please sign in to comment.