Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio
|
3 |
+
import numpy
|
4 |
+
import os
|
5 |
+
import PIL
|
6 |
+
import pillow_heif
|
7 |
+
|
8 |
+
pillow_heif.register_heif_opener()
|
9 |
+
|
10 |
+
class HEIC2Any():
|
11 |
+
def __init__(self):
|
12 |
+
self.title = gradio.Markdown("# HEIC to Any Converter", render = False)
|
13 |
+
self.app = gradio.Blocks()
|
14 |
+
self.input_file = gradio.File(label = "Select HEIC file", file_types = [".heic"], type = "filepath", render = False)
|
15 |
+
self.image_display = gradio.Image(show_download_button = False, render = False)
|
16 |
+
self.convert_button = gradio.Button("Convert", render = False)
|
17 |
+
self.download_button = gradio.DownloadButton("Download", interactive = False, render = False)
|
18 |
+
self.type_mode = gradio.Radio(["jpg", "png"], label = "Select output file type", render = False)
|
19 |
+
self.__render()
|
20 |
+
|
21 |
+
|
22 |
+
def __render(self):
|
23 |
+
with self.app:
|
24 |
+
self.title.render()
|
25 |
+
self.input_file.render()
|
26 |
+
self.type_mode.render()
|
27 |
+
self.convert_button.render()
|
28 |
+
self.image_display.render()
|
29 |
+
self.download_button.render()
|
30 |
+
|
31 |
+
self.__set_behavior()
|
32 |
+
|
33 |
+
def __set_behavior(self):
|
34 |
+
self.convert_button.click(self.convert, inputs = [self.input_file, self.type_mode], outputs = [self.image_display, self.download_button])
|
35 |
+
|
36 |
+
def convert(self, input_file, type_mode):
|
37 |
+
image = PIL.Image.open(input_file)
|
38 |
+
print(type_mode)
|
39 |
+
|
40 |
+
if image is None:
|
41 |
+
self.download_button.interactive = False
|
42 |
+
self.download_button.render()
|
43 |
+
return None
|
44 |
+
|
45 |
+
image = image.convert("RGB")
|
46 |
+
image_np = numpy.array(image)
|
47 |
+
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
48 |
+
|
49 |
+
file_dir = os.path.dirname(input_file)
|
50 |
+
file_name = f"{os.path.basename(input_file).split('.')[0]}.{type_mode}"
|
51 |
+
|
52 |
+
cv2.imwrite(f"{os.path.join(file_dir, file_name)}", image_cv)
|
53 |
+
download_button = gradio.DownloadButton("Download", value = os.path.join(file_dir, file_name), interactive = True)
|
54 |
+
|
55 |
+
return [image_np, download_button]
|
56 |
+
|
57 |
+
def launch(self):
|
58 |
+
self.app.launch(width = 400)
|
59 |
+
|
60 |
+
app = HEIC2Any()
|
61 |
+
app.launch()
|