Quickstart: Search for images using Bing Image Search REST API and Python

Use this quickstart to learn how to send search requests to Bing Image Search API. This Python application sends a search query to the API, and displays the URL of the first image in the results. Although this application is written in Python, the API is a RESTful web service compatible with most programming languages.

To run this example as a Jupyter notebook on MyBinder, select the launch binder badge:


The source code for this sample is available on GitHub with additional error handling and annotations.


Create and initialize the application

  1. Create a new Python file in your favorite IDE or editor, and import the following modules. Create a variable for your subscription key, search endpoint, and search term.

    import requests
    import matplotlib.pyplot as plt
    from PIL import Image
    from io import BytesIO
    subscription_key = "your-subscription-key"
    search_url = "https://api.bing.microsoft.com/v7.0/images/search"
    search_term = "puppies"
  2. Add your subscription key to the Ocp-Apim-Subscription-Key header by creating a dictionary, and adding the key as a value.

    headers = {"Ocp-Apim-Subscription-Key" : subscription_key}

Create and send a search request

  1. Create a dictionary for the search request's parameters. Add your search term to the q parameter. Set the license parameter to public to search for images in the public domain. Set the imageType to photo to search only for photos.

    params  = {"q": search_term, "license": "public", "imageType": "photo"}
  2. Use the requests library to call the Bing Image Search API. Add your header and parameters to the request, and return the response as a JSON object. Get the URLs to several thumbnail images from the response's thumbnailUrl field.

    response = requests.get(search_url, headers=headers, params=params)
    search_results = response.json()
    thumbnail_urls = [img["thumbnailUrl"] for img in search_results["value"][:16]]

View the response

  1. Create a new figure with four columns and four rows by using the matplotlib library.

  2. Iterate through the figure's rows and columns, and use the PIL library's Image.open() method to add an image thumbnail to each space.

  3. Use plt.show() to draw the figure and display the images.

    f, axes = plt.subplots(4, 4)
    for i in range(4):
        for j in range(4):
            image_data = requests.get(thumbnail_urls[i+4*j])
            image = Image.open(BytesIO(image_data.content))        

Example JSON response

Responses from the Bing Image Search API are returned as JSON. This sample response has been truncated to show a single result.

"readLink":"images\/search?q=tropical ocean",
"webSearchUrl":"https:\/\/www.bing.com\/images\/search?q=tropical ocean&FORM=OIIARP",
        "name":"My Life in the Ocean | The greatest WordPress.com site in ...",
        "contentSize":"897388 B",
            "text":"Tropical Beaches Desktop Wallpaper",
            "displayText":"Tropical Beaches Desktop Wallpaper",

Next steps

See also