/
Image_getter.py
112 lines (93 loc) · 3.58 KB
/
Image_getter.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
import random
import urllib.request
import dotenv
import os
import requests
import wget
from os import remove
from shutil import move
from PIL import Image
import numpy as np
dotenv.load_dotenv()
api_key = os.getenv('UNSPLASH_api_key')
query=['Random','Tokyo life','Night city','flowers','Sea','Night sky','stars','Love','Moon','Film','Street Photography','Book','Friendship']
def download_image(url,i):
filename = str(i)+'.jpg'
print(filename)
urllib.request.urlretrieve(url,filename)
def rgb_to_hsv(rgb):
'''Convert Image from RGB to HSV.'''
# Translated from source of colorsys.rgb_to_hsv
# r,g,b should be a numpy arrays with values between 0 and 255
# rgb_to_hsv returns an array of floats between 0.0 and 1.0.
rgb = rgb.astype('float')
hsv = np.zeros_like(rgb)
# in case an RGBA array was passed, just copy the A channel
hsv[..., 3:] = rgb[..., 3:]
r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]
maxc = np.max(rgb[..., :3], axis=-1)
minc = np.min(rgb[..., :3], axis=-1)
hsv[..., 2] = maxc
mask = maxc != minc
hsv[mask, 1] = (maxc - minc)[mask] / maxc[mask]
rc = np.zeros_like(r)
gc = np.zeros_like(g)
bc = np.zeros_like(b)
rc[mask] = (maxc - r)[mask] / (maxc - minc)[mask]
gc[mask] = (maxc - g)[mask] / (maxc - minc)[mask]
bc[mask] = (maxc - b)[mask] / (maxc - minc)[mask]
hsv[..., 0] = np.select(
[r == maxc, g == maxc], [bc - gc, 2.0 + rc - bc], default=4.0 + gc - rc)
hsv[..., 0] = (hsv[..., 0] / 6.0) % 1.0
return hsv
def hsv_to_rgb(hsv):
'''Convert Image from HSV to RGB.'''
# Translated from source of colorsys.hsv_to_rgb
# h,s should be a numpy arrays with values between 0.0 and 1.0
# v should be a numpy array with values between 0.0 and 255.0
# hsv_to_rgb returns an array of uints between 0 and 255.
rgb = np.empty_like(hsv)
rgb[..., 3:] = hsv[..., 3:]
h, s, v = hsv[..., 0], hsv[..., 1], hsv[..., 2]
i = (h * 6.0).astype('uint8')
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
conditions = [s == 0.0, i == 1, i == 2, i == 3, i == 4, i == 5]
rgb[..., 0] = np.select(conditions, [v, q, p, p, t, v], default=v)
rgb[..., 1] = np.select(conditions, [v, v, v, q, p, p], default=t)
rgb[..., 2] = np.select(conditions, [v, p, t, v, v, q], default=p)
return rgb.astype('uint8')
def shift_hue(arr,hout):
hsv=rgb_to_hsv(arr)
hsv[...,0]=hout
rgb=hsv_to_rgb(hsv)
return rgb
def do_it(i):
while(True):
q = query[random.randint(0,len(query)-1)]
call='https://api.unsplash.com/photos/random/?query='+q+'&content_filter=high&orientation=landscape&count=1&featured=true&client_id='+api_key
print('Query='+q)
request = requests.get(call)
data = request.json()
url = data[0]['urls']['full']
print('Saving image')
download_image(url,i)
print('Converting image')
img = Image.open('E:/Python projects/Twitter_LyricBot/'+str(i)+'.jpg').convert('RGBA')
arr = np.array(img)
violet1_hue = 300/360.0
new_img = Image.fromarray(shift_hue(arr,violet1_hue), 'RGBA')
new_img = new_img.convert('RGB')
new_img.save('E:/Python projects/Twitter_LyricBot/'+str(i)+'.jpg')
print('Checking size')
if(os.path.getsize(str(i)+'.jpg')>=3072000):
print('File size was '+str(os.path.getsize(str(i)+'.jpg'))+'. Retrying for a new image')
continue
else:
print('Saved!')
break
for i in range(0,24):
do_it(i)