File size: 2,510 Bytes
8d45028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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()