PGNInfo-test / app.py
andzhk's picture
Create app.py
8d45028
raw
history blame
2.51 kB
import gradio as gr
from PIL import Image
from urllib.request import Request, urlopen
def display_image_from_url(url, input_image):
if url == '' and input_image is None:
return None, "", ""
image = None
if url != '':
req = Request(
url=url,
headers={'User-Agent': 'Mozilla/5.0'}
)
res = urlopen(req)
image = Image.open(res)
image.load()
if input_image is not None:
image = input_image
parameters = "Parameters have been erased from this image or unsupported format"
if 'parameters' in image.info:
parameters = image.info['parameters']
custom_notes = ""
if 'custom_notes' in image.info:
custom_notes = image.info['custom_notes']
return image, parameters, custom_notes, image.info
blocks = gr.Blocks(css="#out_image {height: 400px}")
with blocks as png_info:
with gr.Row():
gr.Markdown(
"""
Report any issues on the [GitHub](https://github.com/andzhik/png-params) page of this project
""")
with gr.Row().style(equal_height=False):
with gr.Column(scale=1):
in_url = gr.Textbox(label="Source URL")
in_image = gr.Image(label="Source Image", type='pil')
with gr.Row():
btn_submit = gr.Button("Submit", variant="primary")
with gr.Column(scale=2):
with gr.Accordion("Image is here") as acc_image:
out_image = gr.Image(type='pil', elem_id="out_image")
out_info = gr.Textbox(label="Generation Parameters")
out_notes = gr.TextArea(label="Custom Notes", interactive=True)
# download_file = gr.File()
btn_save_notes = gr.Button("Save Notes")
# btn_download = gr.Button("Download Image")
with gr.Accordion("Metadata", open=False):
out_meta = gr.Textbox()
btn_submit.click(fn=display_image_from_url,
inputs=[in_url, in_image],
outputs=[out_image, out_info, out_notes, out_meta])
def save_notes(image, custom_notes):
print(custom_notes)
image.info["custom_notes"] = custom_notes
return image
btn_save_notes.click(fn=save_notes,inputs=[out_image, out_notes], outputs=[out_image])
# def download_image(image: Image):
# print(image.info["custom_notes"])
# image.save()
# btn_download.click(None, [out_image], _js="(image)=>{gradioApp().getElementById('out_image')}")
png_info.launch()