File size: 3,214 Bytes
a3b3a2b
 
 
 
01a7219
 
 
a3b3a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a7219
a3b3a2b
 
 
 
 
 
 
 
 
 
 
 
 
9caef19
a3b3a2b
9caef19
a3b3a2b
 
 
 
 
01a7219
 
 
a3b3a2b
 
 
 
 
 
 
8a4fd98
 
 
7e8f329
a3b3a2b
 
 
 
 
 
 
 
 
 
7e8f329
8a4fd98
a3b3a2b
 
8a4fd98
 
 
 
 
 
 
 
 
 
 
 
a3b3a2b
 
 
 
 
01a7219
a3b3a2b
 
 
 
 
01a7219
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from os import rename

from uuid import uuid4

import gradio as gr
from zipfile import ZipFile

output_kind = [
    'Both',
    'Bordered',
    'Borderless'
]

def rename_file(file_path: str) -> str:
    new_file_path = file_path.replace(".png", "") + str(uuid4()) + ".png"
    try: 
        rename(file_path, new_file_path)
        return new_file_path
    except Exception as e:
        print(e)
        return file_path


def extract_snapshots(watchface_link: str, output_type='Both') -> list[str]:
    assert(watchface_link != None)

    with ZipFile(watchface_link, 'r') as zObject:
        if output_type == output_kind[0]:
            snapshot = zObject.extract('snapshot.png', path='outputs')
            bordless_snapshot = zObject.extract('no_borders_snapshot.png', path='outputs')
            return [snapshot, bordless_snapshot]
        elif output_type == output_kind[1]:
            return [zObject.extract('snapshot.png', path='outputs')]
        else:
            return [zObject.extract('no_borders_snapshot.png', path='outputs')]
    

def extract_snapshots_from(watchface_links: list[str], output_kind='Both') -> list[str]:
    assert(watchface_links != None and len(watchface_links) >= 1)

    filtered_watchface_links = [link for link in watchface_links if link.endswith('watchface')]
    snapshots = []
    for watchface_link in filtered_watchface_links:
        images = extract_snapshots(watchface_link, output_kind)
        images = list(map(rename_file, images))
        snapshots += images
    
    return snapshots


with gr.Blocks() as interface:
    gr.Markdown("""
# Welcome to Watchface Extractor
## You can easily extract Wathface previews!
    """)

    with gr.Tab('Single Watchface Output'):
        with gr.Row():
            with gr.Column():
                input_file = gr.File(label="Watchface file", file_count='single', file_types=['watchface'])

                single_button = gr.Button("Extract", variant="primary")

            with gr.Column():
                output_1 = gr.Image(label='Bordered Output', type='filepath')
                output_2 = gr.Image(label='Borderless Output', type='filepath')
    with gr.Tab('Multiple Watchfaces Output'):
        with gr.Row():
            with gr.Column():
                input_files = gr.File(label="Watchface file", file_count='multiple', file_types=['watchface'])
                output_kind_choice = gr.Dropdown(choices=output_kind, value=output_kind[0], label="Output kind for Watchface")

                button = gr.Button("Extract", variant="primary")

            output_gallery = gr.Gallery(label='Output Images')

    single_button.click(
        extract_snapshots,
        inputs=[input_files],
        outputs=[output_1, output_2]
    )

    button.click(
        extract_snapshots_from,
        inputs=[input_files, output_kind_choice],
        outputs=[output_gallery]
    )
    
    input_file.upload(
        fn=extract_snapshots,
        inputs=[input_file],
        outputs=[output_1, output_2]
    )

    input_files.upload(
        fn=extract_snapshots_from,
        inputs=[input_files, output_kind_choice],
        outputs=[output_gallery]
    )

if __name__ == '__main__':
    interface.launch()