Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Factor common parts of saving to different formats using pillow.#21376
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -487,6 +487,16 @@ def print_raw(self, filename_or_obj, *args): | ||
print_rgba = print_raw | ||
def _print_pil(self, filename_or_obj, fmt, pil_kwargs, metadata=None): | ||
""" | ||
Draw the canvas, then save it using `.image.imsave` (to which | ||
*pil_kwargs* and *metadata* are forwarded). | ||
""" | ||
FigureCanvasAgg.draw(self) | ||
mpl.image.imsave( | ||
filename_or_obj, self.buffer_rgba(), format=fmt, origin="upper", | ||
dpi=self.figure.dpi, metadata=metadata, pil_kwargs=pil_kwargs) | ||
@_check_savefig_extra_args | ||
@_api.delete_parameter("3.5", "args") | ||
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. I can't follow if ContributorAuthor 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. No, | ||
def print_png(self, filename_or_obj, *args, | ||
@@ -537,10 +547,7 @@ def print_png(self, filename_or_obj, *args, | ||
If the 'pnginfo' key is present, it completely overrides | ||
*metadata*, including the default 'Software' key. | ||
""" | ||
self._print_pil(filename_or_obj, "png", pil_kwargs, metadata) | ||
def print_to_buffer(self): | ||
FigureCanvasAgg.draw(self) | ||
@@ -555,68 +562,38 @@ def print_to_buffer(self): | ||
@_check_savefig_extra_args() | ||
@_api.delete_parameter("3.5", "args") | ||
def print_jpg(self, filename_or_obj, *args, pil_kwargs=None, **kwargs): | ||
# Remove transparency by alpha-blending on an assumed white background. | ||
r, g, b, a = mcolors.to_rgba(self.figure.get_facecolor()) | ||
try: | ||
self.figure.set_facecolor(a * np.array([r, g, b]) + 1 - a) | ||
self._print_pil(filename_or_obj, "jpeg", pil_kwargs) | ||
finally: | ||
self.figure.set_facecolor((r, g, b, a)) | ||
print_jpeg = print_jpg | ||
@_check_savefig_extra_args | ||
def print_tif(self, filename_or_obj, *, pil_kwargs=None): | ||
self._print_pil(filename_or_obj, "tiff", pil_kwargs) | ||
print_tiff = print_tif | ||
@_check_savefig_extra_args | ||
def print_webp(self, filename_or_obj, *, pil_kwargs=None): | ||
self._print_pil(filename_or_obj, "webp", pil_kwargs) | ||
print_jpg.__doc__, print_tif.__doc__, print_webp.__doc__ = map( | ||
""" | ||
Write the figure to a{} file. | ||
Parameters | ||
---------- | ||
filename_or_obj : str or path-like or file-like | ||
The file to write to. | ||
pil_kwargs : dict, optional | ||
Additional keyword arguments that are passed to | ||
`PIL.Image.Image.save` when saving the figure. | ||
""".format, ["JPEG", "TIFF", "WebP"]) | ||
@_Backend.export | ||