Enhance WebP image saving: add EXIF data and workflow metadata support. Fixes #193

This commit is contained in:
Will Miao
2025-05-21 19:17:12 +08:00
parent 8250acdfb5
commit a073b82751

View File

@@ -379,14 +379,28 @@ class SaveImage:
print(f"Error adding EXIF data: {e}") print(f"Error adding EXIF data: {e}")
img.save(file_path, format="JPEG", **save_kwargs) img.save(file_path, format="JPEG", **save_kwargs)
elif file_format == "webp": elif file_format == "webp":
# For WebP, also use piexif for metadata # For WebP, use exif data in a ComfyUI-compatible format
if metadata: try:
try: # Get exif from image
exif_dict = {'Exif': {piexif.ExifIFD.UserComment: b'UNICODE\0' + metadata.encode('utf-16be')}} exif = img.getexif()
exif_bytes = piexif.dump(exif_dict)
save_kwargs["exif"] = exif_bytes # Add metadata if available
except Exception as e: if metadata:
print(f"Error adding EXIF data: {e}") exif[piexif.ExifIFD.UserComment] = b'UNICODE\0' + metadata.encode('utf-16be')
# Add workflow if embed_workflow is enabled
if embed_workflow and extra_pnginfo is not None:
# Format the workflow with "Workflow:" prefix for ComfyUI compatibility
workflow_json = json.dumps(extra_pnginfo["workflow"])
# Store workflow in ImageDescription tag (0x010e)
exif[piexif.ImageIFD.ImageDescription] = "Workflow:" + workflow_json
# Update save_kwargs with exif data
save_kwargs["exif"] = exif.tobytes()
except Exception as e:
print(f"Error adding EXIF/workflow data to WebP: {e}")
img.save(file_path, format="WEBP", **save_kwargs) img.save(file_path, format="WEBP", **save_kwargs)
results.append({ results.append({