-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Allow saving PNGs through Pillow instead of the builtin _png module. #13207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -453,7 +453,9 @@ def print_raw(self, filename_or_obj, *args, **kwargs): | |
|
||
print_rgba = print_raw | ||
|
||
def print_png(self, filename_or_obj, *args, **kwargs): | ||
def print_png(self, filename_or_obj, *args, | ||
metadata=None, pil_kwargs=None, | ||
**kwargs): | ||
""" | ||
Write the figure to a PNG file. | ||
|
||
|
@@ -494,21 +496,45 @@ def print_png(self, filename_or_obj, *args, **kwargs): | |
.. _PNG specification: \ | ||
https://www.w3.org/TR/2003/REC-PNG-20031110/#11keywords | ||
|
||
pil_kwargs : dict, optional | ||
If set to a non-None value, use Pillow to save the figure instead | ||
of Matplotlib's builtin PNG support, and pass these keyword | ||
arguments to `PIL.Image.save`. | ||
|
||
If the 'pnginfo' key is present, it completely overrides | ||
*metadata*, including the default 'Software' key. | ||
""" | ||
FigureCanvasAgg.draw(self) | ||
renderer = self.get_renderer() | ||
|
||
version_str = ( | ||
'matplotlib version ' + __version__ + ', http://matplotlib.org/') | ||
metadata = OrderedDict({'Software': version_str}) | ||
user_metadata = kwargs.pop("metadata", None) | ||
if user_metadata is not None: | ||
metadata.update(user_metadata) | ||
if metadata is None: | ||
metadata = {} | ||
metadata = { | ||
"Software": | ||
f"matplotlib version{__version__}, http://matplotlib.org/", | ||
**metadata, | ||
} | ||
|
||
with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \ | ||
cbook.open_file_cm(filename_or_obj, "wb") as fh: | ||
_png.write_png(renderer._renderer, fh, | ||
self.figure.dpi, metadata=metadata) | ||
if pil_kwargs is not None: | ||
from PIL import Image | ||
from PIL.PngImagePlugin import PngInfo | ||
buf, size = self.print_to_buffer() | ||
# Only use the metadata kwarg if pnginfo is not set, because the | ||
# semantics of duplicate keys in pnginfo is unclear. | ||
if "pnginfo" not in pil_kwargs: | ||
pnginfo = PngInfo() | ||
for k, v in metadata.items(): | ||
pnginfo.add_text(k, v) | ||
pil_kwargs["pnginfo"] = pnginfo | ||
pil_kwargs.setdefault("dpi", (self.figure.dpi, self.figure.dpi)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little confusing - if the user passes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See discussion in #13094 (review). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right. I even saw that at the time. (though, maybe a comment here though just to remind the unwary). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks fine to me once it passes the test... |
||
(Image.frombuffer("RGBA", size, buf, "raw", "RGBA", 0, 1) | ||
.save(filename_or_obj, format="png", **pil_kwargs)) | ||
|
||
else: | ||
FigureCanvasAgg.draw(self) | ||
renderer = self.get_renderer() | ||
with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \ | ||
cbook.open_file_cm(filename_or_obj, "wb") as fh: | ||
_png.write_png(renderer._renderer, fh, | ||
self.figure.dpi, metadata=metadata) | ||
|
||
def print_to_buffer(self): | ||
FigureCanvasAgg.draw(self) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Randomly surfing this change from a watched issue
Why initing
metadata = {}
is just a line below it's inited to something else again?https://github.com/anntzer/matplotlib/blob/9c7f926d6d70a994c3710ce570bb9807519ef090/lib/matplotlib/backends/backend_agg.py#L509
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used on line 513.