-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetImages.py
More file actions
83 lines (74 loc) · 2.67 KB
/
getImages.py
File metadata and controls
83 lines (74 loc) · 2.67 KB
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
import urllib, httplib, base64
import config
import json
from PIL import Image
from StringIO import StringIO
import requests
class ImageCollector(object):
def __init__(self, subKey):
self.header = {
# Request headers
'Ocp-Apim-Subscription-Key': subKey,
}
def downloadImage(self, url, savePath):
r = requests.get(url)
i = Image.open(StringIO(r.content))
i.save(savePath)
return savePath
def resizeImage(self, imgPath, dim1, dim2):
image = Image.open(imgPath)
image = image.resize((dim1, dim2), Image.ANTIALIAS)
image.save(imgPath)
def getImages(self, query, count):
params = urllib.urlencode({
'q': query,
'count': count,
'offset': '0',
'mkt': 'en-us',
'safeSearch': 'Moderate',
})
try:
conn = httplib.HTTPSConnection('api.cognitive.microsoft.com')
conn.request("GET", "/bing/v5.0/images/search?%s" % params, "{body}", self.header)
response = conn.getresponse()
data = response.read()
conn.close()
return data
except Exception as e:
print e
def parseURL(self, resp):
json_resp = json.loads(resp)
urls = []
for image in json_resp["value"]:
urls.append((image["contentUrl"], image["width"], image["height"]))
return urls
def downloadAllAndResize(self, urls, dim1, dim2, pathBase, count):
pathList = []
counter = 1
for item in urls:
try:
url = item[0]
path = self.downloadImage(url, pathBase + str(counter) + ".jpg")
pathList.append(path)
counter += 1
except Exception as e:
continue
recount = 0
while (counter + recount != count):
img = Image.open(pathList[recount]).copy()
img.save(pathBase + str(counter) + str(recount+1) + ".jpg")
recount += 1
for item in pathList:
self.resizeImage(path, dim1, dim2)
return pathList
def run(self, keyword, count, path, dim1, dim2):
res = self.getImages(keyword, count)
pathList = self.downloadAllAndResize(self.parseURL(res), dim1, dim2, path, count)
return pathList
if __name__ == "__main__":
ic = ImageCollector(config.BING_KEY)
#path = ic.downloadImage("http://parade.com/wp-content/uploads/2014/03/Why-Do-Stars-All-Look-Almost-the-Same-Size-ftr.jpg", "stars.jpg")
#ic.resizeImage(path, 100)
pathList = ic.run("sports", 13, "img/sports", 30, 30)
#print pathList
#ic.getImages("sports", 13)