File size: 2,048 Bytes
975f8e7
c1a5d3b
 
 
975f8e7
c1a5d3b
 
 
 
 
 
975f8e7
c1a5d3b
 
 
975f8e7
c1a5d3b
 
975f8e7
c1a5d3b
 
975f8e7
c1a5d3b
 
 
 
975f8e7
c1a5d3b
 
975f8e7
c1a5d3b
 
 
 
975f8e7
c1a5d3b
975f8e7
 
 
 
 
 
 
 
 
c1a5d3b
 
 
975f8e7
 
 
 
c1a5d3b
 
 
 
975f8e7
 
c1a5d3b
975f8e7
c1a5d3b
975f8e7
 
c1a5d3b
 
 
975f8e7
 
c1a5d3b
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
import gradio as gr
import subprocess
import os
import shutil

# Function to install Rust and Cargo, clone and build avif-decode
def setup_avif_decode():
    # Install Rust and Cargo
    if not os.path.exists(os.path.expanduser("~/.cargo/bin/cargo")):
        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")

    # Clone avif-decode if it doesn't exist
    if not os.path.exists("avif-decode"):
        subprocess.run("git clone https://github.com/kornelski/avif-decode.git", shell=True, check=True)

    # Build avif-decode
    subprocess.run("cd avif-decode && cargo build --release", shell=True, check=True)

# Call setup function to ensure everything is installed and built
setup_avif_decode()

# Define the function to convert AVIF to PNG
def convert_avif_to_png(avif_file):
    avif_path = avif_file.name
    png_path = avif_path.rsplit('.', 1)[0] + '.png'
    
    # Run the avif-decode command
    result = subprocess.run(["avif-decode/target/release/avif_decode", "-f", avif_path, png_path], capture_output=True, text=True)
    
    if result.returncode == 0:
        return png_path
    else:
        return f"Error converting {avif_file.name}: {result.stderr}"

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")

    run_button.click(
        fn=convert_avif_to_png,
        inputs=[avif_file],
        outputs=[result]
    )

demo.queue().launch()