Spaces:
Running
Running
import gradio as gr | |
from PIL import Image | |
import os | |
import ffmpeg | |
gr.themes.Soft() | |
# Ambil daftar format gambar yang didukung oleh Pillow | |
supported_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS', | |
'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM', | |
'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX', | |
'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF'] | |
def convert_image_ffmpeg(image, target_format): | |
try: | |
# Ambil nama file asli tanpa ekstensi | |
base_name = os.path.splitext(os.path.basename(image))[0] | |
# Nama file keluaran dengan awalan yang diinginkan | |
output_file = f"flowly_ai_image_converter_{base_name}.{target_format.lower()}" | |
# Perintah FFmpeg untuk mengonversi gambar | |
ffmpeg.input(image).output(output_file, format=target_format).overwrite_output().run() # Supaya tidak menampilkan output FFmpeg di konsol | |
return output_file | |
except Exception as e: | |
return f"Error: {e}" | |
# Antarmuka Gradio dengan tema kustom untuk menyembunyikan footer | |
interface = gr.Interface( | |
fn=convert_image_ffmpeg, | |
inputs=[ | |
gr.Image(label="Upload Image", type="filepath", height=512), # Atur tinggi gambar input | |
gr.Dropdown(label="Select Target Format", choices=supported_formats) | |
], | |
outputs=gr.File(label="Converted Image"), | |
title="Image Format Converter", | |
description="Upload an image and select any target format for conversion using FFmpeg. Supports popular image formats.", | |
css="footer {visibility: hidden}" | |
) | |
# Jalankan aplikasi | |
if __name__ == "__main__": | |
interface.launch() |