-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Reuse png metadata handling of imsave() in FigureCanvasAgg.print_png(). #15435
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7aad47a
Reuse png metadata handling of imsave() in FigureCanvasAgg.print_png().
anntzer c212f2f
Warn when pil_kwargs["pnginfo"] overrides metadata.
anntzer 83f82f2
Save a couple of numpy conversions.
anntzer 92a33aa
Skip None entries in png metadata.
anntzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1514,18 +1514,37 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, | |
origin = mpl.rcParams["image.origin"] | ||
if origin == "lower": | ||
arr = arr[::-1] | ||
rgba = sm.to_rgba(arr, bytes=True) | ||
if (isinstance(arr, memoryview) and arr.format == "B" | ||
and arr.ndim == 3 and arr.shape[-1] == 4): | ||
# Such an ``arr`` would also be handled fine by sm.to_rgba (after | ||
# casting with asarray), but it is useful to special-case it | ||
# because that's what backend_agg passes, and can be in fact used | ||
# as is, saving a few operations. | ||
rgba = arr | ||
else: | ||
rgba = sm.to_rgba(arr, bytes=True) | ||
if pil_kwargs is None: | ||
pil_kwargs = {} | ||
pil_shape = (rgba.shape[1], rgba.shape[0]) | ||
image = PIL.Image.frombuffer( | ||
"RGBA", pil_shape, rgba, "raw", "RGBA", 0, 1) | ||
if format == "png" and metadata is not None: | ||
# cf. backend_agg's print_png. | ||
pnginfo = PIL.PngImagePlugin.PngInfo() | ||
for k, v in metadata.items(): | ||
pnginfo.add_text(k, v) | ||
pil_kwargs["pnginfo"] = pnginfo | ||
if format == "png": | ||
# Only use the metadata kwarg if pnginfo is not set, because the | ||
# semantics of duplicate keys in pnginfo is unclear. | ||
if "pnginfo" in pil_kwargs: | ||
if metadata: | ||
cbook._warn_external("'metadata' is overridden by the " | ||
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 could also see a case for raising, but warning seems like a step in the right direction. 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 left this as is but can raise too, just pick one. |
||
"'pnginfo' entry in 'pil_kwargs'.") | ||
else: | ||
metadata = { | ||
"Software": (f"Matplotlib version{mpl.__version__}, " | ||
f"https://matplotlib.org/"), | ||
**(metadata if metadata is not None else {}), | ||
QuLogic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
pil_kwargs["pnginfo"] = pnginfo = PIL.PngImagePlugin.PngInfo() | ||
for k, v in metadata.items(): | ||
if v is not None: | ||
pnginfo.add_text(k, v) | ||
if format in ["jpg", "jpeg"]: | ||
format = "jpeg" # Pillow doesn't recognize "jpg". | ||
facecolor = mpl.rcParams["savefig.facecolor"] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.