File size: 2,311 Bytes
dcb2619
 
1490e6b
dcb2619
 
 
5d08e5f
5585d64
 
 
 
dcb2619
 
5585d64
8793ded
5585d64
 
dcb2619
 
 
 
 
8793ded
 
 
 
5585d64
dcb2619
 
8793ded
dcb2619
 
8793ded
 
dcb2619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5585d64
dcb2619
 
 
5585d64
5d08e5f
 
 
 
1490e6b
5d08e5f
14213d1
5d08e5f
 
 
 
 
 
 
 
 
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
import gradio as gr

from src.pixelate import pixelate


def update_palette_visibility(use_palette):
    return gr.update(visible=use_palette), gr.update(visible=use_palette)


with gr.Blocks() as demo:
    gr.Markdown("# Simple Pixelart Filter")

    with gr.Row(equal_height=True):
        with gr.Column(variant="panel"):
            img = gr.Image(label="Input Image",show_fullscreen_button=False)
            pixel_size = gr.Number(label="Pixel Size", minimum=1, value=16)
            blur = gr.Checkbox(label="Blur")
            use_palette = gr.Checkbox(label="Use Palette")
            k = gr.Number(label="Number of Colours", minimum=2, value=8, visible=False)

        with gr.Column(variant="panel"):
            output_img = gr.Image(
                label="Output Image", format="jpeg",
                show_download_button=False,
                show_share_button=False,
                show_fullscreen_button=False
            )
            output_palette = gr.Image(
                label="Image Palette",
                format="jpeg",
                show_download_button=False,
                show_share_button=False,
                show_fullscreen_button=False,
                show_label=False,
                visible=False,
            )

        use_palette.change(
            fn=update_palette_visibility,
            inputs=use_palette,
            outputs=[output_palette, k],
        )

    btn = gr.Button("Pixelate", variant="primary")
    btn.click(
        fn=pixelate,
        inputs=[img, pixel_size, blur, use_palette, k],
        outputs=[output_img, output_palette],
    )

    btn_clear = gr.ClearButton(
        components=[img, blur, output_img, output_palette, use_palette]
    )

    with gr.Column():
        gr.Markdown("## Examples")
        gr.Examples(
            examples=[
                ["example_images/bananas.jpg", 16, False, True, 8],
                ["example_images/scream.jpg", 16, False, True, 4],
                # ["example_images/cat.jpg", 32, True, False, None],
                ["example_images/ducks.jpg", 4, False, True, 2],
            ],
            inputs=[img, pixel_size, blur, use_palette, k],
            outputs=[output_img, output_palette],
            run_on_click=True,
            fn=pixelate,
        )

demo.launch(debug=True)