Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
-
import ffmpeg
|
5 |
-
import tempfile
|
6 |
-
import threading
|
7 |
-
import shutil
|
8 |
|
9 |
-
# Atur tema Gradio
|
10 |
gr.themes.Soft()
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
timer.start()
|
15 |
-
|
16 |
-
# Format gambar yang didukung
|
17 |
-
supported_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
|
18 |
'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
|
19 |
'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX',
|
20 |
-
'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF'
|
21 |
-
|
22 |
-
|
23 |
-
delete_temp_dir(CACHE_DIR, delay=900) # Clean up cache after 15 minutes
|
24 |
-
|
25 |
-
def sanitize_filename(filename):
|
26 |
-
"""Sanitize filename by removing special characters and spaces."""
|
27 |
-
return re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)
|
28 |
-
|
29 |
-
def clean_cache():
|
30 |
-
"""Remove files in the cache directory older than 15 minutes."""
|
31 |
-
now = time.time()
|
32 |
-
for file in os.listdir(CACHE_DIR):
|
33 |
-
file_path = os.path.join(CACHE_DIR, file)
|
34 |
-
if os.path.isfile(file_path) and now - os.path.getmtime(file_path) > 900: # 15 minutes
|
35 |
-
os.remove(file_path)
|
36 |
-
|
37 |
-
def convert_image(image_path, target_format):
|
38 |
try:
|
39 |
-
#
|
40 |
-
|
41 |
-
return "Error: Unsupported target format."
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
|
46 |
-
# Nama file keluaran
|
47 |
-
output_file =
|
48 |
-
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
return output_file # Return path ke file hasil
|
55 |
except Exception as e:
|
56 |
return f"Error: {e}"
|
57 |
-
|
58 |
-
# Antarmuka Gradio
|
59 |
interface = gr.Interface(
|
60 |
fn=convert_image,
|
61 |
inputs=[
|
62 |
-
gr.Image(label="Upload Image", type="filepath", height=512), #
|
63 |
-
gr.Dropdown(label="Select Target Format", choices=supported_formats)
|
64 |
],
|
65 |
-
outputs=gr.File(label="Converted Image"),
|
66 |
title="Image Format Converter",
|
67 |
-
description="Upload an image and select any target format for conversion
|
68 |
-
css="footer {visibility: hidden}"
|
69 |
)
|
70 |
|
71 |
# Jalankan aplikasi
|
72 |
if __name__ == "__main__":
|
73 |
interface.launch()
|
74 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import os
|
|
|
|
|
|
|
|
|
4 |
|
|
|
5 |
gr.themes.Soft()
|
6 |
|
7 |
+
# Ambil daftar format gambar yang didukung oleh Pillow
|
8 |
+
supported_formats = [data for data in (sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
|
|
|
|
|
|
|
|
|
9 |
'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
|
10 |
'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX',
|
11 |
+
'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF',
|
12 |
+
'WEBP', 'WMX', 'XBM']) if data not in ['BLP', 'BUFR', 'GRIB', 'HDF5', 'MSP', 'PALM', 'WMV', 'XBM']]
|
13 |
+
def convert_image(image, target_format):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
+
# Buka file gambar
|
16 |
+
img = Image.open(image)
|
|
|
17 |
|
18 |
+
# Ambil nama file asli tanpa ekstensi
|
19 |
+
base_name = os.path.splitext(os.path.basename(image))[0]
|
20 |
|
21 |
+
# Nama file keluaran dengan awalan yang diinginkan
|
22 |
+
output_file = f"flowly_ai_image_converter_{base_name}.{target_format.lower()}"
|
23 |
+
|
24 |
+
# Simpan gambar dalam format yang dipilih
|
25 |
+
img.save(output_file, format=target_format.upper())
|
26 |
+
|
27 |
+
return output_file
|
|
|
|
|
28 |
except Exception as e:
|
29 |
return f"Error: {e}"
|
30 |
+
|
31 |
+
# Antarmuka Gradio dengan tema kustom untuk menyembunyikan footer
|
32 |
interface = gr.Interface(
|
33 |
fn=convert_image,
|
34 |
inputs=[
|
35 |
+
gr.Image(label="Upload Image", type="filepath", height=512), # Atur tinggi gambar input
|
36 |
+
gr.Dropdown(label="Select Target Format", choices=supported_formats)
|
37 |
],
|
38 |
+
outputs=gr.File(label="Converted Image"),
|
39 |
title="Image Format Converter",
|
40 |
+
description="Upload an image and select any target format for conversion. Supports all formats recognized by Pillow.",
|
41 |
+
css="footer {visibility: hidden}"
|
42 |
)
|
43 |
|
44 |
# Jalankan aplikasi
|
45 |
if __name__ == "__main__":
|
46 |
interface.launch()
|
|