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
2 changes: 1 addition & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def write_png(self, fname):
from matplotlib import _png
im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A,
bytes=True, norm=True)
with open(fname, "wb") as file:
with cbook.open_file_cm(fname, "wb") as file:
_png.write_png(im, file)

def set_data(self, A):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3433,7 +3433,7 @@ def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
from matplotlib import _png
rgba, depth = self.to_rgba(
texstr, color=color, dpi=dpi, fontsize=fontsize)
with open(filename, "wb") as file:
with cbook.open_file_cm(filename, "wb") as file:
_png.write_png(rgba, file)
return depth

Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,14 @@ def test_single_minus_sign():
def test_spaces(fig_test, fig_ref):
fig_test.subplots().set_title(r"$1\,2\>3\ 4$")
fig_ref.subplots().set_title(r"$1\/2\:3~4$")


def test_math_to_image(tmpdir):
mathtext.math_to_image('$x^2$', str(tmpdir.join('example.png')))
mathtext.math_to_image('$x^2$', io.BytesIO())


def test_mathtext_to_png(tmpdir):
mt = mathtext.MathTextParser('bitmap')
mt.to_png(str(tmpdir.join('example.png')), '$x^2$')
mt.to_png(io.BytesIO(), '$x^2$')