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).
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.
The API supports several license types, each catering to different usage scenarios:
Beyond the primary license types, the API also recognizes more granular categories to accommodate specific needs:
For a complete list of license types and their descriptions, refer to the Bing Image Search API Query Parameters.
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.
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}")
requests
library to handle HTTP requests to the Bing Image Search API.bing_image_search
function encapsulates the search logic, allowing for flexible parameterization.search_url
variable holds the endpoint URL for the Bing Image Search API.response.raise_for_status()
."contentUrl"
field.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.
When utilizing license parameters in the Bing Image Search API, consider the following best practices to optimize your search results:
imageType
, size
, and safeSearch
to further refine your search results.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. |
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.