Skip to content

Commit caa677e

Browse files
authored
Merge pull request #16709 from QuLogic/fix-png-write-io
Fix saving PNGs to file objects in some places
2 parents 6993d3c + 5484892 commit caa677e

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/matplotlib/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def write_png(self, fname):
666666
from matplotlib import _png
667667
im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A,
668668
bytes=True, norm=True)
669-
with open(fname, "wb") as file:
669+
with cbook.open_file_cm(fname, "wb") as file:
670670
_png.write_png(im, file)
671671

672672
def set_data(self, A):

lib/matplotlib/mathtext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3433,7 +3433,7 @@ def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
34333433
from matplotlib import _png
34343434
rgba, depth = self.to_rgba(
34353435
texstr, color=color, dpi=dpi, fontsize=fontsize)
3436-
with open(filename, "wb") as file:
3436+
with cbook.open_file_cm(filename, "wb") as file:
34373437
_png.write_png(rgba, file)
34383438
return depth
34393439

lib/matplotlib/tests/test_mathtext.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,14 @@ def test_single_minus_sign():
280280
def test_spaces(fig_test, fig_ref):
281281
fig_test.subplots().set_title(r"$1\,2\>3\ 4$")
282282
fig_ref.subplots().set_title(r"$1\/2\:3~4$")
283+
284+
285+
def test_math_to_image(tmpdir):
286+
mathtext.math_to_image('$x^2$', str(tmpdir.join('example.png')))
287+
mathtext.math_to_image('$x^2$', io.BytesIO())
288+
289+
290+
def test_mathtext_to_png(tmpdir):
291+
mt = mathtext.MathTextParser('bitmap')
292+
mt.to_png(str(tmpdir.join('example.png')), '$x^2$')
293+
mt.to_png(io.BytesIO(), '$x^2$')

0 commit comments

Comments
 (0)