-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_utils.py
152 lines (100 loc) · 3.36 KB
/
tg_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import time
from io import BytesIO
# from StringIO import StringIO
import io
import numpy as np
import requests
from PIL import Image
def sparkey_exists(root):
return os.path.exists(root + '.spi') \
and os.path.exists(root + '.spi')
def ensure_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def avg_image_border(img):
left = np.asarray(img)[0, :]
right = np.asarray(img)[-1, :]
top = np.asarray(img)[:, 0]
bottom = np.asarray(img)[:, -1]
average = np.concatenate((left, right, top, bottom)).mean(axis=0)
return tuple(map(int, average.tolist()))
def pad_image(img, pad):
w, h = img.size
d = max(w, h)
padding = pad if pad is not None else avg_image_border(img)
new_im = Image.new('RGB', (d, d), padding)
w_, h_ = int((d - w) / 2), int((d - h) / 2)
new_im.paste(img, (w_, h_))
return new_im
def open_image_from_path(img_path):
img = Image.open(img_path)
if img.mode != 'RGB':
img = img.convert('RGB')
return img
def open_pad_image_from_path(img_path, pad=None):
img = open_image_from_path(img_path)
return pad_image(img, pad)
def get_image_from_url(img_url):
n_tries = 0
while n_tries < 3:
try:
r = requests.get(img_url, verify=False)
img = Image.open(BytesIO(r.content))
if img.mode != 'RGB':
img = img.convert('RGB')
return img
except requests.exceptions.ConnectionError:
time.sleep(1)
n_tries += 1
print ('here a')
except Exception as e:
print ('here b')
print(e)
break
print('Couldn\'t download image with url: %s' % img_url)
def get_crop_image_from_url(img_url, crop=None):
img = get_image_from_url(img_url)
if img:
if crop is not None:
w, h = img.size
x1, y1 = int(w * crop[0]), int(h * crop[1])
x2, y2 = int(w * crop[2]), int(h * crop[3])
img = img.crop((x1, y1, x2, y2))
return img
def get_pad_image_from_url(img_url, pad=None):
img = get_image_from_url(img_url)
if img:
return pad_image(img, pad)
def get_crop_pad_image_from_url(img_url, crop=None, pad=None):
img = get_crop_image_from_url(img_url, crop)
if img:
return pad_image(img, pad)
def open_crop_pad_image_from_path(img_path, bbox, pad=None):
img = open_image_from_path(img_path)
img = img.crop(tuple(bbox))
return pad_image(img, pad)
def get_image_from_blob(blob):
image = Image.open(io.BytesIO(blob))
if image.mode != 'RGB':
image = image.convert('RGB')
return image
def get_pad_image_from_blob(blob, pad=None):
img = get_image_from_blob(blob)
return pad_image(img, pad)
def get_crop_image_from_blob(blob, crop=None):
""" crop is percentage of full image: x1, y1, x2, y2
"""
img = get_image_from_blob(blob)
if crop is not None:
w, h = img.size
x1, y1 = int(w * crop[0]), int(h * crop[1])
x2, y2 = int(w * crop[2]), int(h * crop[3])
img = img.crop((x1, y1, x2, y2))
return img
def get_crop_pad_image_from_blob(blob, crop=None, pad=None):
img = get_crop_image_from_blob(blob, crop)
return pad_image(img, pad)
def batch_generator(l, n):
for i in xrange(0, len(l), n):
yield l[i:i + n]