Spaces:
Sleeping
Sleeping
File size: 2,785 Bytes
975f8e7 c1a5d3b 7b3419f 975f8e7 115c0bb c1a5d3b 115c0bb c1a5d3b 975f8e7 115c0bb ceb5e3c f5909ff 7b3419f f5909ff 975f8e7 115c0bb 7b3419f f696b8f c9f13a3 f696b8f 7b3419f c1a5d3b 975f8e7 2597f27 975f8e7 c1a5d3b 7b3419f c1a5d3b 2597f27 7b3419f c1a5d3b 7b3419f 2597f27 7b3419f 2597f27 7b3419f b378f1a 7b3419f 975f8e7 7b3419f c1a5d3b 975f8e7 7b3419f b378f1a 7b3419f 115c0bb |
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 |
import gradio as gr
import subprocess
import os
import shutil
import zipfile
def setup_avif_decode():
# Install Rust and Cargo
if not os.path.exists(os.path.expanduser("~/.cargo/bin/cargo")):
subprocess.run("apt-get update && apt-get install -y yasm", shell=True, check=True)
subprocess.run("curl https://sh.rustup.rs -sSf | sh -s -- -y", shell=True, check=True)
os.environ['PATH'] += os.pathsep + os.path.expanduser("~/.cargo/bin")
if not os.path.exists("avif-decode"):
subprocess.run("git clone https://github.com/kornelski/avif-decode.git", shell=True, check=True)
subprocess.run("cd avif-decode && cargo build --release", shell=True, check=True)
setup_avif_decode()
def convert_avif_to_png(avif_files):
output_paths = []
for avif_file in avif_files:
avif_path = avif_file.name
png_path = avif_path.rsplit('.', 1)[0] + '.png'
result = subprocess.run(["avif-decode/target/release/avif_decode", "-f", avif_path, png_path], capture_output=True, text=True)
if result.returncode == 0:
output_paths.append(png_path)
else:
output_paths.append(f"Error converting {avif_file.name}: {result.stderr}")
return output_paths
def zip_files(file_paths):
if not file_paths:
raise gr.Error("No files to download. Please upload and convert AVIF files first.")
zip_path = "/tmp/converted_images.zip"
with zipfile.ZipFile(zip_path, 'w') as zipf:
for file_path in file_paths:
if os.path.isfile(file_path):
zipf.write(file_path, os.path.basename(file_path))
return zip_path
css = """
#col-container {
margin: 0 auto;
max-width: 520px;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("""
# AVIF to PNG Converter
Upload your AVIF files and get them converted to PNG.
""")
with gr.Row():
avif_file = gr.File(
label="Upload AVIF File",
file_types=[".avif"],
file_count="multiple"
)
run_button = gr.Button("Convert", scale=0)
result = gr.Gallery(label="Result")
download_button = gr.Button("Download All as ZIP")
converted_files = []
def handle_conversion(avif_files):
global converted_files
converted_files = convert_avif_to_png(avif_files)
return converted_files
def handle_download():
return zip_files(converted_files)
run_button.click(
fn=handle_conversion,
inputs=[avif_file],
outputs=[result]
)
download_button.click(
fn=handle_download,
inputs=[],
outputs=[gr.File()]
)
demo.queue().launch() |