Skip to content

Commit d910788

Browse files
committed
Save PNG in correct orientation with imsave
1 parent 8279f64 commit d910788

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

lib/matplotlib/image.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,8 @@ def contains(self, mouseevent):
521521

522522
def write_png(self, fname):
523523
"""Write the image to png file with fname"""
524-
im = self.to_rgba(self._A, bytes=True, norm=True)
524+
im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A,
525+
bytes=True, norm=True)
525526
_png.write_png(im, fname)
526527

527528
def set_data(self, A):

lib/matplotlib/tests/test_image.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,24 @@ def test_imsave_color_alpha():
150150
# acceptably preserved through a save/read roundtrip.
151151
from numpy import random
152152
random.seed(1)
153-
data = random.rand(16, 16, 4)
154153

155-
buff = io.BytesIO()
156-
plt.imsave(buff, data)
154+
for origin in ['lower', 'upper']:
155+
data = random.rand(16, 16, 4)
156+
buff = io.BytesIO()
157+
plt.imsave(buff, data, origin=origin)
157158

158-
buff.seek(0)
159-
arr_buf = plt.imread(buff)
159+
buff.seek(0)
160+
arr_buf = plt.imread(buff)
160161

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

167-
assert_array_equal(data, arr_buf)
170+
assert_array_equal(data, arr_buf)
168171

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

0 commit comments

Comments
 (0)