Spaces:
Sleeping
Sleeping
File size: 2,588 Bytes
0f5c80f |
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 |
import gradio as gr
from PIL import Image
from zopflipng import png_optimize
import os
def convert_webp(input_path, compression_level=6, lossless_mode=False):
"""Convert WEBP to PNG with compression options and Zopfli optimization"""
try:
# Convert WEBP to temporary PNG
with Image.open(input_path) as img:
temp_path = "temp.png"
img.convert("RGB").save(
temp_path,
format="PNG",
compress_level=compression_level,
optimize=True
)
# Apply ZopfliPNG optimization if enabled
if lossless_mode:
with open(temp_path, "rb") as f:
original_data = f.read()
optimized_data, code = png_optimize(
original_data,
num_iterations=15,
lossy_8bit=False,
lossy_transparent=False
)
if code == 0:
with open(temp_path, "wb") as f:
f.write(optimized_data)
else:
raise RuntimeError("ZopfliPNG optimization failed")
return temp_path
except Exception as e:
raise gr.Error(f"Conversion failed: {str(e)}")
# Create Gradio interface
with gr.Blocks(title="WEBP to PNG Professional Converter") as app:
gr.Markdown("## 🔄 Professional WEBP to PNG Converter")
with gr.Row():
with gr.Column():
input_img = gr.Image(
type="filepath",
label="Upload WEBP Image",
height=300
)
comp_slider = gr.Slider(
minimum=0,
maximum=9,
value=6,
step=1,
label="PNG Compression Level (0=fast, 9=small)",
info="Standard DEFLATE compression"
)
lossless_toggle = gr.Checkbox(
label="Enable Professional Lossless Mode (ZopfliPNG)",
info="Advanced compression (slower but smaller files)"
)
convert_btn = gr.Button("Start Conversion", variant="primary")
with gr.Column():
output_img = gr.Image(
label="Converted PNG Result",
type="filepath",
height=400,
interactive=False
)
convert_btn.click(
fn=convert_webp,
inputs=[input_img, comp_slider, lossless_toggle],
outputs=output_img
)
if __name__ == "__main__":
app.launch() |