Skip to content

Commit

Permalink
enh: store basic software info in png metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Xarthisius committed Jun 29, 2023
1 parent 0db75b9 commit 4bfc2af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
22 changes: 21 additions & 1 deletion yt/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from more_itertools import always_iterable, collapse, first

from yt._maintenance.deprecation import issue_deprecation_warning
from yt._version import __version__ as yt_version
from yt.config import ytcfg
from yt.units import YTArray, YTQuantity
from yt.utilities.exceptions import YTFieldNotFound, YTInvalidWidthError
Expand Down Expand Up @@ -566,7 +567,7 @@ def get_version_stack():
import matplotlib

version_info = {}
version_info["yt"] = get_yt_version()
version_info["yt"] = yt_version
version_info["numpy"] = np.version.version
version_info["matplotlib"] = matplotlib.__version__
return version_info
Expand Down Expand Up @@ -1447,3 +1448,22 @@ def validate_moment(moment, weight_field):
"Weighted projections can only be made of averages "
"(moment = 1) or standard deviations (moment = 2)!"
)


def setdefault_mpl_metadata(mpl_kwargs, name):
"""
Set a default Software metadata entry for use with Matplotlib outputs.
"""
_, ext = os.path.splitext(name.lower())
if ext in (".eps", ".ps", ".svg", ".pdf"):
key = "Creator"
elif ext == ".png":
key = "Software"
else:
return
default_software = (
"Matplotlib version{matplotlib}, https://matplotlib.org|NumPy-{numpy}|yt-{yt}"
)
metadata = {}
metadata.setdefault(key, default_software.format(**get_version_stack()))
mpl_kwargs.setdefault("metadata", metadata)
10 changes: 9 additions & 1 deletion yt/utilities/png_writer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
from io import BytesIO

import PIL
from PIL import Image
from PIL.PngImagePlugin import PngInfo

from .._version import __version__ as yt_version


def call_png_write_png(buffer, fileobj, dpi):
Image.fromarray(buffer).save(fileobj, dpi=(dpi, dpi), format="png")
metadata = PngInfo()
metadata.add_text("Software", f"{PIL.__name__}-{PIL.__version__}|yt-{yt_version}")
Image.fromarray(buffer).save(
fileobj, dpi=(dpi, dpi), format="png", pnginfo=metadata
)


def write_png(buffer, filename, dpi=100):
Expand Down
9 changes: 8 additions & 1 deletion yt/visualization/base_plot_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import numpy as np
from matplotlib.ticker import LogFormatterMathtext

from yt.funcs import get_interactivity, is_sequence, matplotlib_style_context, mylog
from yt.funcs import (
get_interactivity,
is_sequence,
matplotlib_style_context,
mylog,
setdefault_mpl_metadata,
)
from yt.visualization._handlers import ColorbarHandler, NormHandler

from ._commons import (
Expand Down Expand Up @@ -162,6 +168,7 @@ def save(self, name, mpl_kwargs=None, canvas=None):
mpl_kwargs = {}

name = validate_image_name(name)
setdefault_mpl_metadata(mpl_kwargs, name)

try:
canvas = get_canvas(self.figure, name)
Expand Down

0 comments on commit 4bfc2af

Please sign in to comment.