ThorAILabs commited on
Commit
0f5c80f
·
verified ·
1 Parent(s): 48cb0ce

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from zopflipng import png_optimize
4
+ import os
5
+
6
+ def convert_webp(input_path, compression_level=6, lossless_mode=False):
7
+ """Convert WEBP to PNG with compression options and Zopfli optimization"""
8
+ try:
9
+ # Convert WEBP to temporary PNG
10
+ with Image.open(input_path) as img:
11
+ temp_path = "temp.png"
12
+ img.convert("RGB").save(
13
+ temp_path,
14
+ format="PNG",
15
+ compress_level=compression_level,
16
+ optimize=True
17
+ )
18
+
19
+ # Apply ZopfliPNG optimization if enabled
20
+ if lossless_mode:
21
+ with open(temp_path, "rb") as f:
22
+ original_data = f.read()
23
+
24
+ optimized_data, code = png_optimize(
25
+ original_data,
26
+ num_iterations=15,
27
+ lossy_8bit=False,
28
+ lossy_transparent=False
29
+ )
30
+
31
+ if code == 0:
32
+ with open(temp_path, "wb") as f:
33
+ f.write(optimized_data)
34
+ else:
35
+ raise RuntimeError("ZopfliPNG optimization failed")
36
+
37
+ return temp_path
38
+
39
+ except Exception as e:
40
+ raise gr.Error(f"Conversion failed: {str(e)}")
41
+
42
+ # Create Gradio interface
43
+ with gr.Blocks(title="WEBP to PNG Professional Converter") as app:
44
+ gr.Markdown("## 🔄 Professional WEBP to PNG Converter")
45
+
46
+ with gr.Row():
47
+ with gr.Column():
48
+ input_img = gr.Image(
49
+ type="filepath",
50
+ label="Upload WEBP Image",
51
+ height=300
52
+ )
53
+ comp_slider = gr.Slider(
54
+ minimum=0,
55
+ maximum=9,
56
+ value=6,
57
+ step=1,
58
+ label="PNG Compression Level (0=fast, 9=small)",
59
+ info="Standard DEFLATE compression"
60
+ )
61
+ lossless_toggle = gr.Checkbox(
62
+ label="Enable Professional Lossless Mode (ZopfliPNG)",
63
+ info="Advanced compression (slower but smaller files)"
64
+ )
65
+ convert_btn = gr.Button("Start Conversion", variant="primary")
66
+
67
+ with gr.Column():
68
+ output_img = gr.Image(
69
+ label="Converted PNG Result",
70
+ type="filepath",
71
+ height=400,
72
+ interactive=False
73
+ )
74
+
75
+ convert_btn.click(
76
+ fn=convert_webp,
77
+ inputs=[input_img, comp_slider, lossless_toggle],
78
+ outputs=output_img
79
+ )
80
+
81
+ if __name__ == "__main__":
82
+ app.launch()