Adityadn commited on
Commit
b9a33c4
·
verified ·
1 Parent(s): 02c7e8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -51
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
- def delete_temp_dir(directory, delay=900): # 15 minutes = 900 seconds
13
- timer = threading.Timer(delay, shutil.rmtree, [directory])
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
- CACHE_DIR = tempfile.mkdtemp()
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
- # Pastikan target format valid
40
- if target_format.lower() not in supported_formats:
41
- return "Error: Unsupported target format."
42
 
43
- # Sanitize file path
44
- sanitized_base_name = sanitize_filename(os.path.splitext(os.path.basename(image_path))[0])
45
 
46
- # Nama file keluaran
47
- output_file = os.path.join(CACHE_DIR, f"flowly_ai_image_converter_{sanitized_base_name}.{target_format.lower()}")
48
-
49
- # Gunakan ffmpeg untuk konversi
50
- ffmpeg.input(image_path).output(output_file, format=target_format).overwrite_output().run(
51
- capture_stdout=True, capture_stderr=True
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), # Input gambar
63
- gr.Dropdown(label="Select Target Format", choices=supported_formats) # Pilih format
64
  ],
65
- outputs=gr.File(label="Converted Image"), # Output file hasil
66
  title="Image Format Converter",
67
- description="Upload an image and select any target format for conversion using FFmpeg.",
68
- css="footer {visibility: hidden}" # Sembunyikan footer
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()