-
Notifications
You must be signed in to change notification settings - Fork 50
/
images.py
78 lines (52 loc) · 1.65 KB
/
images.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
import requests
from typing import Union
import cv2
import numpy as np
from io import BytesIO
from sign import sign_url
import os
# from config import *
BERNIE = cv2.imread('bernie.png', -1)
API_URL = os.environ['API_URL']
KEY = os.environ['KEY']
SECRET = os.environ['SECRET']
def get_image(location : Union[str, tuple, int]):
'''
Gets image from `location` in the google street view API
'''
if type(location) is int:
loc_param = 'pano'
else:
loc_param = 'location'
s = requests.Session()
r = requests.Request('GET', API_URL, params={loc_param: location, 'size': '640x640', 'key': KEY}).prepare()
print(r.url)
# print(r.status_code)
signed_url = sign_url(input_url = r.url, secret=SECRET)
print(signed_url)
resp = requests.get(signed_url)
print(resp.status_code)
return resp.content
def add_bernie(img_bytes):
if not img_bytes:
return None
l_img = np.asarray(bytearray(img_bytes), dtype='uint8')
l_img = cv2.imdecode(l_img, cv2.IMREAD_COLOR)
y_offset = 330
x_offset = 400
s_img = BERNIE
y1, y2 = y_offset, y_offset + s_img.shape[0]
x1, x2 = x_offset, x_offset + s_img.shape[1]
alpha_s = s_img[:, :, 3] / 255.0
alpha_l = 1.0 - alpha_s
for c in range(0, 3):
l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] +
alpha_l * l_img[y1:y2, x1:x2, c])
is_success, encoded_bytes = cv2.imencode('.jpeg', l_img)
print(is_success)
img_buf = BytesIO(encoded_bytes)
img_buf.seek(0)
return img_buf
if __name__ == '__main__':
loc = input('Enter an Address: ')
get_image(loc)