Skip to content

Commit c1a3702

Browse files
NelleVQuLogic
authored andcommitted
Merge pull request #7731 from naoyak/writepng-origin-fix
Check origin when saving image to PNG
1 parent 4846dea commit c1a3702

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
@@ -525,7 +525,8 @@ def contains(self, mouseevent):
525525

526526
def write_png(self, fname):
527527
"""Write the image to png file with fname"""
528-
im = self.to_rgba(self._A, bytes=True, norm=True)
528+
im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A,
529+
bytes=True, norm=True)
529530
_png.write_png(im, fname)
530531

531532
def set_data(self, A):

lib/matplotlib/tests/test_image.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,24 @@ def test_imsave_color_alpha():
161161
# acceptably preserved through a save/read roundtrip.
162162
from numpy import random
163163
random.seed(1)
164-
data = random.rand(16, 16, 4)
165164

166-
buff = io.BytesIO()
167-
plt.imsave(buff, data)
165+
for origin in ['lower', 'upper']:
166+
data = random.rand(16, 16, 4)
167+
buff = io.BytesIO()
168+
plt.imsave(buff, data, origin=origin)
168169

169-
buff.seek(0)
170-
arr_buf = plt.imread(buff)
170+
buff.seek(0)
171+
arr_buf = plt.imread(buff)
171172

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

178-
assert_array_equal(data, arr_buf)
181+
assert_array_equal(data, arr_buf)
179182

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

0 commit comments

Comments
 (0)