-
Notifications
You must be signed in to change notification settings - Fork 0
/
original.py
76 lines (58 loc) · 1.61 KB
/
original.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
'''
Created on Jul 28, 2016
@author: Vladimir
'''
from viewer import Viewer
from PIL import Image
import json
import os
import sys
import requests
png_files = []
image_files = []
api_key = '2902398-0284227c6439d51a2b4352a7c'
img_type = 'green+python' # то, что ищем
def error(msg='One or more images are not converted'):
print(msg)
exit(0)
def convert_images():
for infile in image_files:
f, e = os.path.splitext(infile)
outfile = f + ".png"
if infile != outfile:
try:
Image.open(infile).save(outfile)
png_files.append(outfile)
os.remove(infile)
except IOError:
print("cannot convert", infile)
def show_slides():
viewer = Viewer(png_files)
viewer.show_slides()
viewer.run()
def get_images():
main_url = u'https://pixabay.com/api/?key={}&q={}&image_type=photo=pretty=true'.format(api_key, img_type)
response = requests.get(main_url).content.decode('utf8')
hits = json.loads(response)
directory = 'downloaded_images'
if not os.path.exists(directory):
os.mkdir(directory)
if not hits:
sys.exit("Found No Images")
count = 0
for hit in hits['hits']:
count += 1
url = hit['webformatURL']
res = requests.get(url)
name = str(count) + '.jpg'
path = r'downloaded_images/{}'.format(name)
image_files.append(path)
f = open(path, 'wb')
f.write(res.content)
f.close()
def test():
get_images()
convert_images()
show_slides()
if __name__ == '__main__':
test()