namelessai commited on
Commit
03672ff
·
verified ·
1 Parent(s): 2d78155

Add application files.

Browse files
Files changed (2) hide show
  1. app.py +75 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import tempfile
4
+ from PIL import Image
5
+ import pillow_heif
6
+ import numpy as np
7
+
8
+ def convert_heic_to_png(heic_file):
9
+ """
10
+ Convert HEIC image to PNG format with lossless compression
11
+
12
+ Args:
13
+ heic_file (str): Path to the HEIC file
14
+
15
+ Returns:
16
+ str: Path to the converted PNG file
17
+ """
18
+ try:
19
+ # Register HEIF opener with Pillow
20
+ pillow_heif.register_heif_opener()
21
+
22
+ # Open the HEIC file
23
+ img = Image.open(heic_file)
24
+
25
+ # Create a temporary file for the output PNG
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp:
27
+ # Save as PNG with maximum compression (lossless)
28
+ img.save(tmp.name, 'PNG', compress_level=9)
29
+ return tmp.name
30
+
31
+ except Exception as e:
32
+ raise gr.Error(f"Error converting HEIC to PNG: {str(e)}")
33
+
34
+ def process_heic_files(files):
35
+ """
36
+ Process one or more HEIC files and return download links for the converted PNGs
37
+ """
38
+ if not files:
39
+ return None
40
+
41
+ output_files = []
42
+ for file in files:
43
+ try:
44
+ png_path = convert_heic_to_png(file.name)
45
+ output_files.append(png_path)
46
+ except Exception as e:
47
+ print(f"Error processing {file.name}: {str(e)}")
48
+
49
+ return output_files if output_files else None
50
+
51
+ # Create the Gradio interface
52
+ demo = gr.Interface(
53
+ fn=process_heic_files,
54
+ inputs=gr.File(
55
+ file_count="multiple",
56
+ file_types=[".heic", ".HEIC", ".heif", ".HEIF"],
57
+ label="Upload HEIC/HEIF Files"
58
+ ),
59
+ outputs=gr.Files(label="Download Converted PNGs"),
60
+ title="HEIC to PNG Converter",
61
+ description="Upload one or more HEIC/HEIF images to convert them to lossless PNG format.",
62
+ allow_flagging="never"
63
+ )
64
+
65
+ if __name__ == "__main__":
66
+ # Print installation instructions if pillow_heif is not available
67
+ try:
68
+ import pillow_heif
69
+ except ImportError:
70
+ print("Error: pillow_heif is required but not installed.")
71
+ print("Please install it using: pip install pillow-heif")
72
+ exit(1)
73
+
74
+ # Run the Gradio app
75
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=3.0.0
2
+ pillow>=9.0.0
3
+ pillow-heif>=0.7.0
4
+ numpy>=1.20.0