Adityadn commited on
Commit
aec01ff
·
verified ·
1 Parent(s): aa3b442

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -1,29 +1,32 @@
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
 
14
- def convert_image(image, target_format):
15
  try:
16
- # Buka file gambar
17
- img = Image.open(image)
18
-
19
  # Ambil nama file asli tanpa ekstensi
20
  base_name = os.path.splitext(os.path.basename(image))[0]
21
 
22
  # Nama file keluaran dengan awalan yang diinginkan
23
  output_file = f"flowly_ai_image_converter_{base_name}.{target_format.lower()}"
24
 
25
- # Simpan gambar dalam format yang dipilih
26
- img.save(output_file, format=target_format.upper())
 
 
 
 
 
 
27
 
28
  return output_file
29
  except Exception as e:
@@ -31,17 +34,17 @@ def convert_image(image, target_format):
31
 
32
  # Antarmuka Gradio dengan tema kustom untuk menyembunyikan footer
33
  interface = gr.Interface(
34
- fn=convert_image,
35
  inputs=[
36
  gr.Image(label="Upload Image", type="filepath", height=512), # Atur tinggi gambar input
37
  gr.Dropdown(label="Select Target Format", choices=supported_formats)
38
  ],
39
  outputs=gr.File(label="Converted Image"),
40
- title="Image Format Converter",
41
- description="Upload an image and select any target format for conversion. Supports all formats recognized by Pillow.",
42
  css="footer {visibility: hidden}"
43
  )
44
 
45
  # Jalankan aplikasi
46
  if __name__ == "__main__":
47
- interface.launch()
 
1
  import gradio as gr
2
  from PIL import Image
3
  import os
4
+ import ffmpeg
5
 
6
  gr.themes.Soft()
7
 
8
  # Ambil daftar format gambar yang didukung oleh Pillow
9
+ supported_formats = (sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
10
  'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
11
  'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX',
12
  'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF',
 
13
 
14
+ def convert_image_ffmpeg(image, target_format):
15
  try:
 
 
 
16
  # Ambil nama file asli tanpa ekstensi
17
  base_name = os.path.splitext(os.path.basename(image))[0]
18
 
19
  # Nama file keluaran dengan awalan yang diinginkan
20
  output_file = f"flowly_ai_image_converter_{base_name}.{target_format.lower()}"
21
 
22
+ # Perintah FFmpeg untuk mengonversi gambar
23
+ (
24
+ ffmpeg
25
+ .input(image)
26
+ .output(output_file, format=target_format)
27
+ .overwrite_output() # Overwrite jika file sudah ada
28
+ .run(quiet=True) # Supaya tidak menampilkan output FFmpeg di konsol
29
+ )
30
 
31
  return output_file
32
  except Exception as e:
 
34
 
35
  # Antarmuka Gradio dengan tema kustom untuk menyembunyikan footer
36
  interface = gr.Interface(
37
+ fn=convert_image_ffmpeg,
38
  inputs=[
39
  gr.Image(label="Upload Image", type="filepath", height=512), # Atur tinggi gambar input
40
  gr.Dropdown(label="Select Target Format", choices=supported_formats)
41
  ],
42
  outputs=gr.File(label="Converted Image"),
43
+ title="Image Format Converter (FFmpeg)",
44
+ description="Upload an image and select any target format for conversion using FFmpeg. Supports popular image formats.",
45
  css="footer {visibility: hidden}"
46
  )
47
 
48
  # Jalankan aplikasi
49
  if __name__ == "__main__":
50
+ interface.launch()