From 633867ecc6061c547dd62533dad2077fa0d6ac08 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sat, 27 May 2023 19:06:49 +0300 Subject: [PATCH] fix serving images that have already been saved without temp files function that broke after updating gradio --- modules/ui_tempdir.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/ui_tempdir.py b/modules/ui_tempdir.py index f05049e1f..7f6b42aea 100644 --- a/modules/ui_tempdir.py +++ b/modules/ui_tempdir.py @@ -4,6 +4,7 @@ from collections import namedtuple from pathlib import Path import gradio as gr +import gradio.components from PIL import PngImagePlugin @@ -31,13 +32,16 @@ def check_tmp_file(gradio, filename): return False -def save_pil_to_file(pil_image, dir=None): +def save_pil_to_file(self, pil_image, dir=None): already_saved_as = getattr(pil_image, 'already_saved_as', None) if already_saved_as and os.path.isfile(already_saved_as): register_tmp_file(shared.demo, already_saved_as) + filename = already_saved_as - file_obj = Savedfile(f'{already_saved_as}?{os.path.getmtime(already_saved_as)}') - return file_obj + if not shared.opts.save_images_add_number: + filename += f'?{os.path.getmtime(already_saved_as)}' + + return filename if shared.opts.temp_dir != "": dir = shared.opts.temp_dir @@ -51,11 +55,11 @@ def save_pil_to_file(pil_image, dir=None): file_obj = tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir=dir) pil_image.save(file_obj, pnginfo=(metadata if use_metadata else None)) - return file_obj + return file_obj.name # override save to file function so that it also writes PNG info -gr.processing_utils.save_pil_to_file = save_pil_to_file +gradio.components.IOComponent.pil_to_temp_file = save_pil_to_file def on_tmpdir_changed():