From 34cf684419f5d7774244ec555944a95678dd4756 Mon Sep 17 00:00:00 2001 From: JJ Date: Mon, 12 Sep 2022 21:40:02 +1000 Subject: [PATCH] add metadata to jpg and non-png image files * needs a piexif module install * dumps the info in an Exif "UserComment" * update to webui.bat --- modules/images.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/images.py b/modules/images.py index fc9a01130..27d558ede 100644 --- a/modules/images.py +++ b/modules/images.py @@ -5,6 +5,8 @@ from collections import namedtuple import re import numpy as np +import piexif +import piexif.helper from PIL import Image, ImageFont, ImageDraw, PngImagePlugin from fonts.ttf import Roboto import string @@ -323,7 +325,14 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i if not os.path.exists(fullfn): break - image.save(fullfn, quality=opts.jpeg_quality, pnginfo=pnginfo) + if extension == "png": + image.save(fullfn, quality=opts.jpeg_quality, pnginfo=pnginfo) + else: + exif_dict = { "Exif" : dict() } + exif_dict["Exif"][piexif.ExifIFD.UserComment] = piexif.helper.UserComment.dump( + info, encoding="unicode") + exif_bytes = piexif.dump(exif_dict) + image.save(fullfn, quality=opts.jpeg_quality, exif=exif_bytes) target_side_length = 4000 oversize = image.width > target_side_length or image.height > target_side_length @@ -335,7 +344,7 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i elif oversize: image = image.resize((image.width * target_side_length // image.height, target_side_length), LANCZOS) - image.save(f"{fullfn_without_extension}.jpg", quality=opts.jpeg_quality, pnginfo=pnginfo) + image.save(fullfn, quality=opts.jpeg_quality, exif=exif_bytes) if opts.save_txt and info is not None: with open(f"{fullfn_without_extension}.txt", "w", encoding="utf8") as file: