forked from jantic/DeOldify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_utils.py
126 lines (94 loc) · 3.39 KB
/
app_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import requests
import random
import _thread as thread
from uuid import uuid4
import numpy as np
import skimage
from skimage.filters import gaussian
from PIL import Image
def compress_image(image, path_original):
size = 1920, 1080
width = 1920
height = 1080
name = os.path.basename(path_original).split('.')
first_name = os.path.join(os.path.dirname(path_original), name[0] + '.jpg')
if image.size[0] > width and image.size[1] > height:
image.thumbnail(size, Image.ANTIALIAS)
image.save(first_name, quality=85)
elif image.size[0] > width:
wpercent = (width/float(image.size[0]))
height = int((float(image.size[1])*float(wpercent)))
image = image.resize((width,height), PIL.Image.ANTIALIAS)
image.save(first_name,quality=85)
elif image.size[1] > height:
wpercent = (height/float(image.size[1]))
width = int((float(image.size[0])*float(wpercent)))
image = image.resize((width,height), Image.ANTIALIAS)
image.save(first_name, quality=85)
else:
image.save(first_name, quality=85)
def convertToJPG(path_original):
img = Image.open(path_original)
name = os.path.basename(path_original).split('.')
first_name = os.path.join(os.path.dirname(path_original), name[0] + '.jpg')
if img.format == "JPEG":
image = img.convert('RGB')
compress_image(image, path_original)
img.close()
elif img.format == "GIF":
i = img.convert("RGBA")
bg = Image.new("RGBA", i.size)
image = Image.composite(i, bg, i)
compress_image(image, path_original)
img.close()
elif img.format == "PNG":
try:
image = Image.new("RGB", img.size, (255,255,255))
image.paste(img,img)
compress_image(image, path_original)
except ValueError:
image = img.convert('RGB')
compress_image(image, path_original)
img.close()
elif img.format == "BMP":
image = img.convert('RGB')
compress_image(image, path_original)
img.close()
def blur(image, x0, x1, y0, y1, sigma=1, multichannel=True):
y0, y1 = min(y0, y1), max(y0, y1)
x0, x1 = min(x0, x1), max(x0, x1)
im = image.copy()
sub_im = im[y0:y1,x0:x1].copy()
blur_sub_im = gaussian(sub_im, sigma=sigma, multichannel=multichannel)
blur_sub_im = np.round(255 * blur_sub_im)
im[y0:y1,x0:x1] = blur_sub_im
return im
def download(url, filename):
data = requests.get(url).content
with open(filename, 'wb') as handler:
handler.write(data)
return filename
def generate_random_filename(upload_directory, extension):
filename = str(uuid4())
filename = os.path.join(upload_directory, filename + "." + extension)
return filename
def clean_me(filename):
if os.path.exists(filename):
os.remove(filename)
def clean_all(files):
for me in files:
clean_me(me)
def create_directory(path):
os.makedirs(os.path.dirname(path), exist_ok=True)
def get_model_bin(url, output_path):
if not os.path.exists(output_path):
create_directory(output_path)
cmd = "wget -O %s %s" % (output_path, url)
print(cmd)
os.system(cmd)
return output_path
#model_list = [(url, output_path), (url, output_path)]
def get_multi_model_bin(model_list):
for m in model_list:
thread.start_new_thread(get_model_bin, m)