Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
15 changes: 11 additions & 4 deletions scripts/gradio_gallery_monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
from PIL import Image
import gradio as gr
from modules.shared import opts

# It's possible we could just use gradio.processing_utils.encode_pil_to_base64
# But that does something-or-other with metadata; I don't want to test it, and this is short and works.
Expand All @@ -15,10 +16,16 @@ def img_to_base64uri(image):
if not isinstance(image, Image.Image):
raise NotImplementedError # We don't handle other types

pseudofile = BytesIO()
image.save(pseudofile, format="PNG")
base64repr = base64.b64encode(pseudofile.getvalue())
return f"data:image/png;base64,{base64repr.decode('utf8')}"
with BytesIO() as pseudofile:
if opts.enable_pnginfo:
metadata = gr.processing_utils.get_pil_metadata(image)

if metadata is None:
image.save(pseudofile, format="PNG")
else:
image.save(pseudofile, format="PNG", pnginfo=metadata)
base64repr = base64.b64encode(pseudofile.getvalue())
return f"data:image/png;base64,{base64repr.decode('utf8')}"

# Do a monkey-patch on the gradio gallery postprocess function to make it output base64 strings instead.
old_gallery_postprocess = gr.Gallery.postprocess
Expand Down