Skip to content

Commit eae4154

Browse files
committed
Warn when pil_kwargs["pnginfo"] overrides metadata.
1 parent b771a56 commit eae4154

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,6 @@ def print_png(self, filename_or_obj, *args,
494494
If the 'pnginfo' key is present, it completely overrides
495495
*metadata*, including the default 'Software' key.
496496
"""
497-
498-
if metadata is None:
499-
metadata = {}
500-
metadata = {
501-
"Software":
502-
f"matplotlib version{mpl.__version__}, http://matplotlib.org/",
503-
**metadata,
504-
}
505497
FigureCanvasAgg.draw(self)
506498
mpl.image.imsave(
507499
filename_or_obj, np.asarray(self.buffer_rgba()), format="png",

lib/matplotlib/image.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,14 +1527,22 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15271527
pil_shape = (rgba.shape[1], rgba.shape[0])
15281528
image = PIL.Image.frombuffer(
15291529
"RGBA", pil_shape, rgba, "raw", "RGBA", 0, 1)
1530-
if (format == "png"
1531-
and metadata is not None and "pnginfo" not in pil_kwargs):
1530+
if format == "png":
15321531
# Only use the metadata kwarg if pnginfo is not set, because the
15331532
# semantics of duplicate keys in pnginfo is unclear.
1534-
pnginfo = PIL.PngImagePlugin.PngInfo()
1535-
for k, v in metadata.items():
1536-
pnginfo.add_text(k, v)
1537-
pil_kwargs["pnginfo"] = pnginfo
1533+
if "pnginfo" in pil_kwargs:
1534+
if metadata:
1535+
cbook._warn_external("'metadata' is overridden by the "
1536+
"'pnginfo' entry in 'pil_kwargs'.")
1537+
else:
1538+
metadata = {
1539+
"Software": (f"matplotlib version{mpl.__version__}, "
1540+
f"http://matplotlib.org/"),
1541+
**(metadata if metadata is not None else {}),
1542+
}
1543+
pil_kwargs["pnginfo"] = pnginfo = PIL.PngImagePlugin.PngInfo()
1544+
for k, v in metadata.items():
1545+
pnginfo.add_text(k, v)
15381546
if format in ["jpg", "jpeg"]:
15391547
format = "jpeg" # Pillow doesn't recognize "jpg".
15401548
color = tuple(

0 commit comments

Comments
 (0)