Here is a small python script I used to download images from bing search engine. An extra test is performed to check if the image has the focal length in the exif information. This information is mandatory for bundler so the download system will skip the images which are not matching.
There already exists libraries to get search result from bing: pybing and bingapi. They both work pretty well. I decided to use pybing. So let's install it:
sudo easy_install pybing
The next step is to install the python imaging library in order to capture the EXIF information:
sudo easy_install PIL
Script
- from pybing.query.query import BingQuery
- from pybing.resultset import BingResultSet
- from pybing.query import BingQuery
- from pybing.query import Pagable
- import Image
- import ExifTags
- import os
- import sys
- __author__ = 'jacques fontignie'
- from pybing import *
- APP_ID = <ENTER_YOUR_APP_ID>
- exifAttrs = dict(Model=True,Make=True,ExifImageWidth=True,ExifImageHeight=True,FocalLength=True)
- class ImageQuery(BingQuery, Pagable):
- SOURCE_TYPE = constants.IMAGE_SOURCE_TYPE
- def get_request_parameters(self):
- params = super(ImageQuery, self).get_request_parameters()
- params.update({
- 'Image.Filters': 'Style:Photo'
- })
- return params
- def download(dir, filename, url):
- """Copy the contents of a file from a given URL
- to a local file.
- """
- import urllib2
- try:
- webFile = urllib2.urlopen(url)
- except:
- print "Unexpected error:", sys.exc_info()[0]
- return False
- extension = url.split('.')[-1].lower()
- if not extension == "jpg":
- print "not a jpg extension: " + url
- return False
- localPath = dir + "/" + filename + "." + extension
- localFile = open(localPath, 'w')
- localFile.write(webFile.read())
- webFile.close()
- localFile.close()
- print "File successfully downloaded"
- try:
- photoHandle = Image.open(localPath)
- found = False
- exif = {}
- info = photoHandle._getexif()
- if info:
- for attr, value in info.items():
- decodedAttr = ExifTags.TAGS.get(attr, attr)
- if decodedAttr in exifAttrs: exif[decodedAttr] = value
- if 'FocalLength' in exif:
- found = True
- if not found:
- print "Dropping the file as it does not contain any useful exif information"
- os.remove(localPath)
- return False
- except:
- print "Unexpected error:", sys.exc_info()[0]
- os.remove(localPath)
- return False
- # get EXIF information as a dictionary
- return True
- def main():
- dir = "images"
- if not os.path.exists(dir):
- os.makedirs(dir)
- query = ImageQuery(APP_ID,query="rushmore")
- results = query.execute()
- default_name = "img_"
- index = 0
- count = 0
- for result in results:
- count = count +1
- url = result.mediaurl
- print (str(index) + ") " + str(count) + "- " + url)
- filename = default_name + '%05d' % index
- if download(dir,filename,url):
- index = index + 1
- if index == 500:
- break
- print "Images succesfully fetched ({})".format(index)
- if __name__ == '__main__':
- main()
man could you help me?
RépondreSupprimeri got this error... File "/home/panda/mybing2.py", line 103, in
main()
File "/home/panda/mybing2.py", line 85, in main
for result in results:
File "/usr/local/lib/python2.7/dist-packages/pybing-0.12-py2.7.egg/pybing/resultset.py", line 116, in __iter__
for result in query.get_search_results():
File "/usr/local/lib/python2.7/dist-packages/pybing-0.12-py2.7.egg/pybing/query/query.py", line 76, in get_search_results
response = self.get_search_response()
File "/usr/local/lib/python2.7/dist-packages/pybing-0.12-py2.7.egg/pybing/query/query.py", line 72, in get_search_response
return json.loads(contents)['SearchResponse'][self.SOURCE_TYPE]