Skip to content

Instantly share code, notes, and snippets.

@aymericdelab
Created October 16, 2019 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aymericdelab/1f0eaa578f2de329a9eb1d441c7d4725 to your computer and use it in GitHub Desktop.
Save aymericdelab/1f0eaa578f2de329a9eb1d441c7d4725 to your computer and use it in GitHub Desktop.
from azure.cognitiveservices.search.imagesearch import ImageSearchAPI
from msrest.authentication import CognitiveServicesCredentials
from PIL import Image
import requests
from io import BytesIO
import os
# create an Azure account
# and get your API key from here:
# https://azure.microsoft.com/en-us/services/cognitive-services/bing-image-search-api/
subscription_key = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
founders = ["Jeff Bezos","Bill Gates","Larry Page"]
offsets=[0,150,300,450]
client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key))
for founder in founders:
prefix=r'.\data\bing\{}'.format(founder)
os.makedirs(prefix)
count=0
for offset in offsets:
image_results = client.images.search(
query=founder,
count=150,
offset=offset,
image_content='Face',
image_type='Photo')
for result in image_results.value:
url=result.content_url
try:
response = requests.get(url)
img = Image.open(BytesIO(response.content))
path=prefix+'\{}.{}.jpg'.format(count,founder)
img.save(path)
count+=1
except:
print('Connection error or wrong format')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment