Chat
Search
Ithy Logo

Understanding Bing Image Search API License Parameters and Types

Comprehensive guide to filtering images by license with Python examples

code on screen

Key Takeaways

  • License Filtering: The Bing Image Search API provides robust license filtering options to retrieve images that meet specific usage rights.
  • Multiple License Types: Users can filter images based on various license types, including public domain, shareable, and commercial usage.
  • Python Integration: Practical Python examples demonstrate how to implement license parameters effectively in API requests.

Introduction to Bing Image Search API

The Bing Image Search API is a powerful tool provided by Microsoft that allows developers to integrate image search functionalities into their applications. One of its key features is the ability to filter search results based on image licenses, ensuring that the images retrieved comply with specific usage rights and requirements (Microsoft Docs).

Understanding License Parameters

The license parameter in the Bing Image Search API is instrumental in filtering images according to their licensing terms. This ensures that users can obtain images that are legally permissible for their intended use, whether for personal projects, commercial applications, or content modification.

Primary License Types

The API supports several license types, each catering to different usage scenarios:

  • Any: Retrieves all images regardless of their license.
  • Public: Filters results to include only images in the public domain.
  • Share: Includes images that can be freely shared with some restrictions.
  • ShareCommercially: Filters images that can be shared and used for commercial purposes.
  • Modify: Retrieves images that can be modified.
  • ModifyCommercially: Filters images that can be both modified and used commercially.

Extended License Types

Beyond the primary license types, the API also recognizes more granular categories to accommodate specific needs:

  • Type-Any: Includes all Creative Commons licenses.
  • L1: Public Domain.
  • L2 to L7: Various levels of free sharing and usage rights, including modification and commercial use.

For a complete list of license types and their descriptions, refer to the Bing Image Search API Query Parameters.

Using the License Parameter in Python

Integrating the license parameter into your Python applications allows for precise control over the images retrieved from the Bing Image Search API. Below is a detailed Python example demonstrating how to implement this.

Python Example: Filtering Images by License

import requests

def bing_image_search(subscription_key, query, license_type='public', image_type='photo', count=10):
    """
    Searches Bing Images with specified license and image type parameters.

    Parameters:
        subscription_key (str): Your Bing Image Search API subscription key.
        query (str): Search query term.
        license_type (str): License type for filtering images.
        image_type (str): Type of images to search for.
        count (int): Number of search results to return.

    Returns:
        list: A list of image URLs matching the search criteria.
    """
    search_url = "https://api.bing.microsoft.com/v7.0/images/search"
    headers = {"Ocp-Apim-Subscription-Key": subscription_key}
    params = {
        "q": query,
        "license": license_type,
        "imageType": image_type,
        "count": count
    }

    response = requests.get(search_url, headers=headers, params=params)
    response.raise_for_status()
    search_results = response.json()
    image_urls = [image["contentUrl"] for image in search_results.get("value", [])]
    return image_urls

if __name__ == "__main__":
    subscription_key = "YOUR_SUBSCRIPTION_KEY"
    search_query = "sunsets"
    license_filter = "public"  # Options: any, public, share, shareCommercially, modify, modifyCommercially
    image_category = "photo"
    number_of_results = 10

    images = bing_image_search(subscription_key, search_query, license_filter, image_category, number_of_results)
    for idx, url in enumerate(images, start=1):
        print(f"{idx}: {url}")
    

Explanation of the Code

  1. Importing Libraries: The script uses the requests library to handle HTTP requests to the Bing Image Search API.
  2. Function Definition: The bing_image_search function encapsulates the search logic, allowing for flexible parameterization.
  3. API Endpoint: The search_url variable holds the endpoint URL for the Bing Image Search API.
  4. Headers and Parameters: The request headers include the subscription key, while the parameters define the search query, license type, image type, and the number of results.
  5. Making the Request: A GET request is sent to the API endpoint with the specified headers and parameters. The response is checked for success using response.raise_for_status().
  6. Parsing the Response: The JSON response is parsed to extract the list of image URLs from the "contentUrl" field.
  7. Output: The script prints out the URLs of the retrieved images.

Advanced Usage: Combining Multiple License Parameters

For more refined searches, you can combine multiple license parameters to narrow down results further. Here’s how you can modify the function to accept multiple license types:

def bing_image_search_combined_licenses(subscription_key, query, license_types, image_type='photo', count=10):
    """
    Searches Bing Images with multiple specified license types.

    Parameters:
        subscription_key (str): Your Bing Image Search API subscription key.
        query (str): Search query term.
        license_types (list): List of license types for filtering images.
        image_type (str): Type of images to search for.
        count (int): Number of search results to return.

    Returns:
        list: A list of image URLs matching the search criteria.
    """
    search_url = "https://api.bing.microsoft.com/v7.0/images/search"
    headers = {"Ocp-Apim-Subscription-Key": subscription_key}
    # Join multiple licenses with comma as per API documentation
    license_filter = ",".join(license_types)
    params = {
        "q": query,
        "license": license_filter,
        "imageType": image_type,
        "count": count
    }

    response = requests.get(search_url, headers=headers, params=params)
    response.raise_for_status()
    search_results = response.json()
    image_urls = [image["contentUrl"] for image in search_results.get("value", [])]
    return image_urls

if __name__ == "__main__":
    subscription_key = "YOUR_SUBSCRIPTION_KEY"
    search_query = "nature landscapes"
    license_filters = ["public", "shareCommercially"]  # Example: Public domain and shareable commercially
    image_category = "photo"
    number_of_results = 15

    images = bing_image_search_combined_licenses(subscription_key, search_query, license_filters, image_category, number_of_results)
    for idx, url in enumerate(images, start=1):
        print(f"{idx}: {url}")
    

This enhanced function allows users to specify multiple license types by passing a list of licenses, which the function then joins into a comma-separated string as required by the API.

Best Practices for Using License Parameters

When utilizing license parameters in the Bing Image Search API, consider the following best practices to optimize your search results:

  • Understand Your Requirements: Clearly define the usage rights you need for the images, whether for commercial use, modification, or distribution.
  • Combine Filters: Utilize additional parameters like imageType, size, and safeSearch to further refine your search results.
  • Handle API Limits: Be aware of the API usage limits and implement error handling to manage rate limiting or quota exceedances.
  • Validate Image Licenses: Always verify the license information provided in the API response to ensure compliance with usage rights.

Common License Parameter Values

License Type Description
Any Includes all images regardless of license.
Public Images in the public domain.
Share Images that can be freely shared with some restrictions.
ShareCommercially Images that can be shared and used for commercial purposes.
Modify Images that can be modified.
ModifyCommercially Images that can be modified and used commercially.

References


Recap

The Bing Image Search API offers a comprehensive set of license parameters that empower developers to retrieve images tailored to specific usage rights. By effectively utilizing the license parameter in conjunction with other search filters, you can ensure that the images integrated into your applications comply with legal and ethical standards. The provided Python examples serve as practical guides for implementing these features, enabling seamless and compliant image searches.


Last updated January 11, 2025
Ask Ithy AI
Export Article
Delete Article