Spaces:
Sleeping
Sleeping
Update app.py
Browse files- Now adds ability to extract previews from multiple watchfaces!!
- Tabbed interface for single and multiple
app.py
CHANGED
|
@@ -1,21 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from zipfile import ZipFile
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
with ZipFile(watchface_link, 'r') as zObject:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
with gr.Blocks() as interface:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
if __name__ == '__main__':
|
| 21 |
interface.launch()
|
|
|
|
| 1 |
+
from os import rename
|
| 2 |
+
|
| 3 |
+
from uuid import uuid4
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
from zipfile import ZipFile
|
| 7 |
|
| 8 |
+
output_kind = [
|
| 9 |
+
'Both',
|
| 10 |
+
'Bordered',
|
| 11 |
+
'Borderless'
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
def rename_file(file_path: str) -> str:
|
| 15 |
+
new_file_path = file_path.replace(".png", "") + str(uuid4()) + ".png"
|
| 16 |
+
try:
|
| 17 |
+
rename(file_path, new_file_path)
|
| 18 |
+
return new_file_path
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(e)
|
| 21 |
+
return file_path
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def extract_snapshots(watchface_link: str, output_type='Both') -> list[str]:
|
| 25 |
+
assert(watchface_link != None)
|
| 26 |
+
|
| 27 |
with ZipFile(watchface_link, 'r') as zObject:
|
| 28 |
+
if output_type == output_kind[0]:
|
| 29 |
+
snapshot = zObject.extract('snapshot.png', path='outputs')
|
| 30 |
+
bordless_snapshot = zObject.extract('no_borders_snapshot.png', path='outputs')
|
| 31 |
+
return [snapshot, bordless_snapshot]
|
| 32 |
+
elif output_type == output_kind[1]:
|
| 33 |
+
return [zObject.extract('snapshot.png', path='outputs')]
|
| 34 |
+
else:
|
| 35 |
+
return [zObject.extract('no_borders_snapshot.png', path='outputs')]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def extract_snapshots_from(watchface_links: list[str], output_kind='Both') -> list[str]:
|
| 39 |
+
assert(watchface_links != None and len(watchface_links) >= 1)
|
| 40 |
+
|
| 41 |
+
snapshots = []
|
| 42 |
+
for watchface_link in watchface_links:
|
| 43 |
+
images = extract_snapshots(watchface_link, output_kind)
|
| 44 |
+
images = list(map(rename_file, images))
|
| 45 |
+
snapshots += images
|
| 46 |
+
|
| 47 |
+
return snapshots
|
| 48 |
|
| 49 |
|
| 50 |
with gr.Blocks() as interface:
|
| 51 |
+
gr.Markdown("""
|
| 52 |
+
# Welcome to Watchface Extractor
|
| 53 |
+
## You can easily extract Wathface previews!
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
with gr.Tab('Single Watchface Output'):
|
| 57 |
+
with gr.Row():
|
| 58 |
+
input_file = gr.File(label="Watchface file", file_count='single', file_types=['watchface'])
|
| 59 |
+
|
| 60 |
+
with gr.Column():
|
| 61 |
+
output_1 = gr.Image(label='Bordered Output', type='filepath')
|
| 62 |
+
output_2 = gr.Image(label='Borderless Output', type='filepath')
|
| 63 |
+
with gr.Tab('Multiple Watchfaces Output'):
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column():
|
| 66 |
+
input_files = gr.File(label="Watchface file", file_count='multiple', file_types=['watchface'])
|
| 67 |
+
output_kind_choice = gr.Dropdown(choices=output_kind, value=output_kind[0], label="Output kind for Watchface")
|
| 68 |
+
|
| 69 |
+
output_gallery = gr.Gallery(label='Output Images')
|
| 70 |
+
|
| 71 |
+
input_file.upload(
|
| 72 |
+
fn=extract_snapshots,
|
| 73 |
+
inputs=[input_file],
|
| 74 |
+
outputs=[output_1, output_2]
|
| 75 |
+
)
|
| 76 |
|
| 77 |
+
input_files.upload(
|
| 78 |
+
fn=extract_snapshots_from,
|
| 79 |
+
inputs=[input_files, output_kind_choice],
|
| 80 |
+
outputs=[output_gallery]
|
| 81 |
+
)
|
| 82 |
|
| 83 |
if __name__ == '__main__':
|
| 84 |
interface.launch()
|