Some checks failed
Python Linting / Run Ruff (push) Has been cancelled
Python Linting / Run Pylint (push) Has been cancelled
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.10, [self-hosted Linux], stable) (push) Has been cancelled
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.11, [self-hosted Linux], stable) (push) Has been cancelled
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.12, [self-hosted Linux], stable) (push) Has been cancelled
Full Comfy CI Workflow Runs / test-unix-nightly (12.1, , linux, 3.11, [self-hosted Linux], nightly) (push) Has been cancelled
Execution Tests / test (macos-latest) (push) Has been cancelled
Execution Tests / test (ubuntu-latest) (push) Has been cancelled
Execution Tests / test (windows-latest) (push) Has been cancelled
Test server launches without errors / test (push) Has been cancelled
Unit Tests / test (macos-latest) (push) Has been cancelled
Unit Tests / test (ubuntu-latest) (push) Has been cancelled
Unit Tests / test (windows-2022) (push) Has been cancelled
Includes 30 custom nodes committed directly, 7 Civitai-exclusive loras stored via Git LFS, and a setup script that installs all dependencies and downloads HuggingFace-hosted models on vast.ai. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
64 lines
3.0 KiB
Python
64 lines
3.0 KiB
Python
from typing import Any, cast
|
|
from PIL.PngImagePlugin import PngInfo
|
|
from PIL.Image import Image
|
|
|
|
import json
|
|
import piexif
|
|
import piexif.helper
|
|
|
|
def save_image(image: Image, filepath: str, extension: str, quality_jpeg_or_webp: int, lossless_webp: bool, optimize_png: bool, a111_params: str, prompt: dict[str, Any] | None, extra_pnginfo: dict[str, Any] | None, embed_workflow: bool) -> None:
|
|
if extension == 'png':
|
|
metadata = PngInfo()
|
|
if a111_params:
|
|
metadata.add_text("parameters", a111_params)
|
|
|
|
if embed_workflow:
|
|
if extra_pnginfo is not None:
|
|
for k, v in extra_pnginfo.items():
|
|
metadata.add_text(k, json.dumps(v, separators=(',', ':')))
|
|
if prompt is not None:
|
|
metadata.add_text("prompt", json.dumps(prompt, separators=(',', ':')))
|
|
|
|
image.save(filepath, pnginfo=metadata, optimize=optimize_png)
|
|
else: # webp & jpeg
|
|
image.save(filepath, optimize=True, quality=quality_jpeg_or_webp, lossless=lossless_webp)
|
|
|
|
# Native example adding workflow to exif:
|
|
# https://github.com/comfyanonymous/ComfyUI/blob/095610717000bffd477a7e72988d1fb2299afacb/comfy_extras/nodes_images.py#L113
|
|
pnginfo_json = {}
|
|
prompt_json = {}
|
|
if embed_workflow:
|
|
if extra_pnginfo is not None:
|
|
pnginfo_json = {piexif.ImageIFD.Make - i: f"{k}:{json.dumps(v, separators=(',', ':'))}" for i, (k, v) in enumerate(extra_pnginfo.items())}
|
|
if prompt is not None:
|
|
prompt_json = {piexif.ImageIFD.Model: f"prompt:{json.dumps(prompt, separators=(',', ':'))}"}
|
|
|
|
def get_exif_bytes() -> bytes:
|
|
exif_dict = ({
|
|
"0th": pnginfo_json | prompt_json
|
|
} if pnginfo_json or prompt_json else {}) | ({
|
|
"Exif": {
|
|
piexif.ExifIFD.UserComment: cast(bytes, piexif.helper.UserComment.dump(a111_params, encoding="unicode"))
|
|
},
|
|
} if a111_params else {})
|
|
return cast(bytes, piexif.dump(exif_dict))
|
|
|
|
exif_bytes = get_exif_bytes()
|
|
|
|
# JPEG format limits the EXIF bytes to a maximum of 65535 bytes
|
|
if extension == "jpg" or extension == "jpeg":
|
|
MAX_EXIF_SIZE = 65535
|
|
if len(exif_bytes) > MAX_EXIF_SIZE and embed_workflow:
|
|
print("ComfyUI-Image-Saver: Error: Workflow is too large, removing client request prompt.")
|
|
prompt_json = {}
|
|
exif_bytes = get_exif_bytes()
|
|
if len(exif_bytes) > MAX_EXIF_SIZE:
|
|
print("ComfyUI-Image-Saver: Error: Workflow is still too large, cannot embed workflow!")
|
|
pnginfo_json = {}
|
|
exif_bytes = get_exif_bytes()
|
|
if len(exif_bytes) > MAX_EXIF_SIZE:
|
|
print("ComfyUI-Image-Saver: Error: Metadata exceeds maximum size for JPEG. Cannot save metadata.")
|
|
return
|
|
|
|
piexif.insert(exif_bytes, filepath)
|