Skip to content

Commit

Permalink
fix serving images that have already been saved without temp files fu…
Browse files Browse the repository at this point in the history
…nction that broke after updating gradio
  • Loading branch information
AUTOMATIC1111 committed May 27, 2023
1 parent dd37763 commit 6095ade
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions modules/ui_tempdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

import gradio as gr
import gradio.components

from PIL import PngImagePlugin

Expand Down Expand Up @@ -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
Expand All @@ -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

This comment has been minimized.

Copy link
@akx

akx May 28, 2023

Collaborator

In the future, we could probably fix this sort of breakage with

assert hasattr(gradio.components.IOComponent, "pil_to_temp_file")

so we'd never patch something that isn't there in the first place.



def on_tmpdir_changed():
Expand Down

2 comments on commit 6095ade

@TimonFugier
Copy link

@TimonFugier TimonFugier commented on 6095ade Jun 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is creating a problem with stable-diffusion-webui-two-shot :
opparco/stable-diffusion-webui-two-shot#54

To make a quick recap :
The function img_array_to_temp_file calls pil_to_temp_file (who indeed has the format parameter) in Automatic1111\stable-diffusion-webui\venv\Lib\site-packages\gradio\components.py
self.pil_to_temp_file(pil_image, dir, format="png")
BUT
This function pil_to_temp_file has been overwritten in Automatic1111\stable-diffusion-webui\modules\ui_tempdir.py
# override save to file function so that it also writes PNG info
gradio.components.IOComponent.pil_to_temp_file = save_pil_to_file
AND
The function save_pil_to_file that overwrites pil_to_temp_file does not have a "format" parameter hence the error.

@kotaro5487
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mattyamonaca/PBRemTools#42

It seems that a similar issue has occurred in this link as well.

Please sign in to comment.