From 0cd74602531a40f72d1a75b471a8a9166135d333 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Wed, 26 Oct 2022 13:12:44 +0300 Subject: add script callback for before image save and change callback for after image save to use a class with parameters --- modules/images.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'modules/images.py') diff --git a/modules/images.py b/modules/images.py index bfc2ba06..7870b5b7 100644 --- a/modules/images.py +++ b/modules/images.py @@ -451,17 +451,6 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i """ namegen = FilenameGenerator(p, seed, prompt) - if extension == 'png' and opts.enable_pnginfo and info is not None: - pnginfo = PngImagePlugin.PngInfo() - - if existing_info is not None: - for k, v in existing_info.items(): - pnginfo.add_text(k, str(v)) - - pnginfo.add_text(pnginfo_section_name, info) - else: - pnginfo = None - if save_to_dirs is None: save_to_dirs = (grid and opts.grid_save_to_dirs) or (not grid and opts.save_to_dirs and not no_prompt) @@ -489,19 +478,27 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i if add_number: basecount = get_next_sequence_number(path, basename) fullfn = None - fullfn_without_extension = None for i in range(500): fn = f"{basecount + i:05}" if basename == '' else f"{basename}-{basecount + i:04}" fullfn = os.path.join(path, f"{fn}{file_decoration}.{extension}") - fullfn_without_extension = os.path.join(path, f"{fn}{file_decoration}") if not os.path.exists(fullfn): break else: fullfn = os.path.join(path, f"{file_decoration}.{extension}") - fullfn_without_extension = os.path.join(path, file_decoration) else: fullfn = os.path.join(path, f"{forced_filename}.{extension}") - fullfn_without_extension = os.path.join(path, forced_filename) + + pnginfo = existing_info or {} + if info is not None: + pnginfo[pnginfo_section_name] = info + + params = script_callbacks.ImageSaveParams(image, p, fullfn, pnginfo) + script_callbacks.before_image_saved_callback(params) + + image = params.image + fullfn = params.filename + info = params.pnginfo.get(pnginfo_section_name, None) + fullfn_without_extension, extension = os.path.splitext(params.filename) def exif_bytes(): return piexif.dump({ @@ -510,12 +507,20 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i }, }) - if extension.lower() in ("jpg", "jpeg", "webp"): + if extension.lower() == '.png': + pnginfo_data = PngImagePlugin.PngInfo() + for k, v in params.pnginfo.items(): + pnginfo_data.add_text(k, str(v)) + + image.save(fullfn, quality=opts.jpeg_quality, pnginfo=pnginfo_data) + + elif extension.lower() in (".jpg", ".jpeg", ".webp"): image.save(fullfn, quality=opts.jpeg_quality) + if opts.enable_pnginfo and info is not None: piexif.insert(exif_bytes(), fullfn) else: - image.save(fullfn, quality=opts.jpeg_quality, pnginfo=pnginfo) + image.save(fullfn, quality=opts.jpeg_quality) target_side_length = 4000 oversize = image.width > target_side_length or image.height > target_side_length @@ -538,7 +543,8 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i else: txt_fullfn = None - script_callbacks.image_saved_callback(image, p, fullfn, txt_fullfn) + script_callbacks.image_saved_callback(params) + return fullfn, txt_fullfn -- cgit v1.2.1 From 539c0f51e436beeb0ca2b8b8d52b24f4b59ad56a Mon Sep 17 00:00:00 2001 From: Yaiol <38218161+Yaiol@users.noreply.github.com> Date: Sat, 29 Oct 2022 01:07:01 +0200 Subject: Update images.py Filename tags [height] and [width] are wrongly referencing to process size instead of resulting image size. Making all upscale files named wrongly. --- modules/images.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'modules/images.py') diff --git a/modules/images.py b/modules/images.py index 7870b5b7..a0728553 100644 --- a/modules/images.py +++ b/modules/images.py @@ -300,8 +300,8 @@ class FilenameGenerator: 'seed': lambda self: self.seed if self.seed is not None else '', 'steps': lambda self: self.p and self.p.steps, 'cfg': lambda self: self.p and self.p.cfg_scale, - 'width': lambda self: self.p and self.p.width, - 'height': lambda self: self.p and self.p.height, + 'width': lambda self: self.image.width, + 'height': lambda self: self.image.height, 'styles': lambda self: self.p and sanitize_filename_part(", ".join([style for style in self.p.styles if not style == "None"]) or "None", replace_spaces=False), 'sampler': lambda self: self.p and sanitize_filename_part(sd_samplers.samplers[self.p.sampler_index].name, replace_spaces=False), 'model_hash': lambda self: getattr(self.p, "sd_model_hash", shared.sd_model.sd_model_hash), @@ -315,10 +315,11 @@ class FilenameGenerator: } default_time_format = '%Y%m%d%H%M%S' - def __init__(self, p, seed, prompt): + def __init__(self, p, seed, prompt, image): self.p = p self.seed = seed self.prompt = prompt + self.image = image def prompt_no_style(self): if self.p is None or self.prompt is None: @@ -449,7 +450,7 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i txt_fullfn (`str` or None): If a text file is saved for this image, this will be its full path. Otherwise None. """ - namegen = FilenameGenerator(p, seed, prompt) + namegen = FilenameGenerator(p, seed, prompt, image) if save_to_dirs is None: save_to_dirs = (grid and opts.grid_save_to_dirs) or (not grid and opts.save_to_dirs and not no_prompt) -- cgit v1.2.1 From 8792be50078c2e89ac2fa8ff4e4c1ca82f140d45 Mon Sep 17 00:00:00 2001 From: timntorres Date: Mon, 31 Oct 2022 17:29:04 -0700 Subject: Add PNG info to pngs only if option is enabled. --- modules/images.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'modules/images.py') diff --git a/modules/images.py b/modules/images.py index a0728553..ae705cbd 100644 --- a/modules/images.py +++ b/modules/images.py @@ -510,8 +510,9 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i if extension.lower() == '.png': pnginfo_data = PngImagePlugin.PngInfo() - for k, v in params.pnginfo.items(): - pnginfo_data.add_text(k, str(v)) + if opts.enable_pnginfo: + for k, v in params.pnginfo.items(): + pnginfo_data.add_text(k, str(v)) image.save(fullfn, quality=opts.jpeg_quality, pnginfo=pnginfo_data) -- cgit v1.2.1