Files
LacklusterOpsecandVito Sansevero 9943a61619 fix(images): link WebP images to their prompts (#650)
* fix(images): link WebP and AVIF images to their prompts

ComfyUI writes the workflow and the prompt into PNG text chunks, and the
library read only those. SaveAnimatedWEBP writes the same records as EXIF
instead: `prompt:{json}` at tag 0x0110, then `workflow:{json}` and every
other `extra_pnginfo` key from 0x010F down; the AVIF writer uses that
layout. Pillow answers them only through `getexif()`, so an animated WebP
or an AVIF scanned into the library matched no prompt and stayed unlinked.

`utils/exif_text.py` answers the `png_text` shape from those tags, and
both metadata readers (the scan's `_extract_comfyui_metadata` and the
monitor's `extract_metadata`) ask it when a file has no text chunks. Only a
value shaped like a record counts, so camera EXIF stays out. PNG is
unchanged: its records are text chunks and `exif_text` answers `{}`.

The tests write real still and animated WebP, AVIF (where Pillow has it) and
JPEG files in ComfyUI's layout, and hold both readers to them.

* docs(images): AVIF is read but not yet collected (#652)

The EXIF reader handles ComfyUI's AVIF files, verified against files
written by ComfyUI's own _save_avif. No scan, monitor or gallery list
collects .avif yet, so say so and point at #652.

---------

Co-authored-by: Vito Sansevero <vito.sansevero@gmail.com>
2026-11-19 01:00:17 +00:00

48 lines
1.9 KiB
Python

"""ComfyUI's EXIF metadata records (WebP, AVIF, JPEG).
PNG keeps the workflow and the prompt in text chunks (``utils.png_text``).
The formats without text chunks carry the same records as EXIF strings:
``SaveAnimatedWEBP`` writes ``prompt:{json}`` to tag 0x0110, then
``workflow:{json}`` and every other ``extra_pnginfo`` key to 0x010F and down,
and the AVIF writer uses that layout too. Pillow answers those tags only
through ``getexif()``; ``image.info`` holds the raw EXIF blob, so reading it
as text found no metadata at all.
``exif_text`` returns the ``{key: json}`` shape of ``utils.png_text`` so a
caller reads either source the same way. Only a value shaped like a ComfyUI
record counts, a key and a colon followed by a JSON object, array or string;
a camera's ``Make`` or a ``DateTime`` is not metadata.
"""
import re
# A record's key: ``workflow``, ``prompt`` or an extra_pnginfo key.
_KEY = re.compile(r"[A-Za-z_][A-Za-z0-9_]*\Z")
# What a record holds: the workflow and the prompt are objects, the pair
# records a list, a custom extra_pnginfo value may be a string. Numbers,
# dates and camera names start with anything else.
_STARTS = ("{", "[", '"')
def exif_text(image) -> dict:
"""ComfyUI's EXIF records of an opened image, or ``{}``.
The image stays usable and nothing is decoded but the EXIF block. Formats
whose records are text chunks (PNG) answer ``{}``; read those with
``utils.png_text.png_text`` first.
"""
try:
exif = image.getexif()
except Exception: # a broken EXIF block never fails a scan
return {}
records = {}
for value in exif.values():
if isinstance(value, bytes):
value = value.decode("utf-8", errors="replace")
if not isinstance(value, str):
continue
key, separator, payload = value.partition(":")
if separator and _KEY.match(key) and payload.lstrip()[:1] in _STARTS:
records[key] = payload
return records