Getting more than 150 images using search_images_bing

Any idea how I can obtain more than 150 images using search_images_bing?

I tried redefining the function using ctn param for number of images:
def search_images_bing2(key, term, min_sz=128,cnt=150):
client = api(‘’, auth(key))
return L(, count=cnt, min_height=min_sz, min_width=min_sz).value)

But I still got 150 images using
results = search_images_bing2(key,f'{o} men',cnt=500)


I had the exact same question!

According to the Bing Image Search API reference, the maximum images you can get in one request is 150 images.

However, using the count and offset query parameters, we can page through and get 150 images at a time through multiple requests.

After some debugging, I’ve made the following changes to the search_images_bing() function in the file:

# +
# pip install azure-cognitiveservices-search-imagesearch

from itertools import chain

from import ImageSearchClient as api
from msrest.authentication import CognitiveServicesCredentials as auth

def search_images_bing(key, term, total_count=150, min_sz=128):
    """Search for images using the Bing API
    :param key: Your Bing API key
    :type key: str
    :param term: The search term to search for
    :type term: str
    :param total_count: The total number of images you want to return (default is 150)
    :type total_count: int
    :param min_sz: the minimum height and width of the images to search for (default is 128)
    :type min_sz: int
    :returns: An L-collection of ImageObject
    :rtype: L
    max_count = 150
    client = api("", auth(key))
    imgs = [
            query=term, min_height=min_sz, min_width=min_sz, count=count, offset=offset
        for count, offset in (
                max_count if total_count - offset > max_count else total_count - offset,
            for offset in range(0, total_count, max_count)
    return L(chain(*imgs))

You can now use the search_images_bing() function in your notebooks as follows:

from utils import *

results = search_images_bing(key, "grizzly bear", 500)

I have not been able to find if there is a maximum to the number of times we can apply an offset. For example, if Bing runs out of images. So I don’t know what will happen if you request an extremely large number of images (10,000+) and Bing has less than that amount. But as long as you don’t hit that limit, you should be OK.

Please test and let me know how it works for you. If anyone has any suggestions to improve the code, I’d love to know! If it works well, I may put in a pull request to the course repo.


Good Stuff!
Thank you for posting @oddrationale , it worked well for me.

Could you give me a hand and explain how I can change in the file using gradient?
(I ran your code by copying to the notebook and overriding the function)

I’m not using Gradient, but I think the process should be the same. In the folder that has the chapter notebooks, if you scroll down, you should see a file. You can open that file and find the search_image_bing() function and overwrite the code.

Just came across the same issue and wondered if anyone already solved it. Thank you @oddrationale!

1 Like

·Hi, can you give me a Help for this?

Im already have my Key from Azure, im statring to do this change that you comment on But after make a search im still appear an a empty folder.


@Alfiesan, let’s check to make sure the image_search_bing() function returned images as we expected.

key = "<YOUR_KEY>"
results = search_images_bing(key, 'grizzly bear')

Run that in a cell and make sure results contains ImageObject. If it does, then we’re good. Run download_images() and double-check that the path you passed into get_image_files() is correct.

If results is empty, then we have a problem and probably need more troubleshooting. So let me know.

@oddrationale, I really like how you have extended search_images_bing() to download more than 150 images from bing.

@jeremy would it make sense to incude this extended version of search_images_bing() within fastai/fastai/vision/ I really like the functionality provided by this PR. I understand that it doesn’t make sense to include within fastai/fastbook/ I think finding images for training vision models to be an appropriate addition to fastai itself, as it can be a core process to creating vision models.

I also created a function search_images_dgd and a PR #250 which might be considered to be included there as well.

1 Like

@oddrationale, as the number of downloads increase, one needs to account for duplicate images as shown here, where len(list(set(ims))) finds unique urls:

for max_n in [100,150,200,500,1000,2000]:
    results = search_images_bing(key, 'grizzly bear',max_n)
    ims = results.attrgot('content_url')
    print(max_n, len(ims), len(list(set(ims))))
100 100 100
150 150 150
200 200 168
500 500 409
1000 954 788
2000 1280 787

To fix this, you would either need to break the api of search_images_bin() or else have the user check after calling the function. This is not an issue with for original function, as when calling less than 150 images, there duplications do not appear to be a problem.


Thanks, @prairieguy, for the constructive feedback! I appreciate you testing it so thoroughly.

To me, I see that you brought up two separate issues:

  1. Bing Search API does not always return the count that give it, and
  2. Duplicate content_url's can be returned.

I think these should be handled separately.

Bing Search API returns less images

I did some additional testing and I believe this occurs because we’ve reached the maximum Bing will return.

results = L("grizzly bear", min_height=128, min_width=128, count=150, offset=1500).value)



Here I called the function directly, without using search_images_bing(). I used a count of 150 and a high offset of 1500. Only 54 items were returned.

Let’s try again with a higher offset.

results2 = L("grizzly bear", min_height=128, min_width=128, count=150, offset=1800).value)



After increasing the offset, I got the same same number of results as before. I did some random sampling and these two lists seem to have the same content_url's.



I think what is going on here is that once Bing has reached the maximum number of images it has for that search term, it will continue to return the same set of images after a certain offset.

One solution for this is to perhaps just show a warning message to the user, but return the images anyways. Something like:

import logging

if len(L(chain(*imgs))) < total_count:
    logging.warning(f"Bing only found {len(L(chain(*imgs)))} images for '{term}'. Total requested was {total_count}.")

Duplicate image URLs

For the second issue of duplicate image URLs, I think it should be the user’s responsibility to remove the duplicates. The search_images_bing() function should just return the images that Bing provides. It would be analogous to browsing Bing Image Search manually and seeing duplicate images. The user can decide to keep or remove the duplicate image URLs.

Again, I appreciate you taking the time to read and test my code, which is why I wanted to in turn give my detailed response. Would love to get your thoughts on this and I’ll modify the snippet in my original posting accordingly so that others can benefit!

1 Like

@oddrationale, a couple of comments:

  • It does appear that there is a relatively low number of images available. Adding a warning as you described makes sense, provide the fewer number of results are still returned.

  • Since it appears that your PR was not accepted, I think it’s less important that this function needs to maintain the original api.

  • I would change it to include the L.attrogot(‘content_url’) within search_images_bing. I think this makes sense as this as the user shouldn’t have to remember why and what that call is about. I think the function should just return a L of image urls. It seem like a cleaner interface to me. This function returns image urls and download_images handles the downloading.

  • Finally, I do think that it would make sense to remove duplicates. I can’t think of many reasons the user would want duplicate images. Moreover, I for one, wouldn’t want to remember to remove duplicates.

  • I went ahead and re-wrote the code to reflect these changes. I changed the style to my personal taste, but I believe it reflects your original logic.

def search_images_bing(key, term, total_count=150, min_sz=128):
    """Search for images using the Bing API
    :param key: Your Bing API key
    :type key: str
    :param term: The search term to search for
    :type term: str
    :param total_count: The total number of imagles you want to return (default is 150)
    :type total_count: int
    :param min_sz: the minimum height and width of the images to search for (default is 128)
    :type min_sz: int
    :returns: An L-collection of ImageObject
    :rtype: L
    max_count = 150
    client = api("", auth(key))
    imgs = []                                                                                                                                         
    for offset in range(0, total_count, max_count):
        count = max_count if total_count - offset > max_count else total_count - offset
        img =,min_height=min_sz,min_width=min_sz,count=count,offset=offset).value
    return L(chain(*imgs)).attrgot('content_url').unique()

Regarding duplicates, the other issue I sometimes face with my own scraper is not duplicate URLs, but duplicate images coming from different places, for example if you search for Van Gogh paintings, you’re going to get very many copies of starry night and sunflowers.

I’m planning on writing some duplicate image detection code as soon as I get time but if anyone had already done this, please speak up.

1 Like

@joedockrill - I have written the function dedupe_images(image_dir:Path)->int: ( which takes a directory of images as inputs and remove duplicates, returning how many were deleted. It does so at a binary level, so this would not detect different images of the same painting, only multiple identical images. (This happens relatively frequently.)

In the same file, filter_images(image_dir:Path, img_type:str='JPEG')->int will remove non-image files from a directory.

The dependencies for these functions are hashlib and magic

1 Like

I was thinking more something that would say, this is a different size, slightly different brightness/contrast, and maybe even slightly different crop, but I think it’s the same picture. Here are all the images i think are the same and the one I think you should keep, press buttons please.

I think I know how to go about it, I just haven’t had time to do anything about it yet.

Sounds like a difficult problem. Not sure how I would even tackle it. Perhaps a neural network itself in which you manually curate training examples in which you train for “sameness”. Otherwise, sounds like lots of case checking if images.

What were you thinking as a general strategy?

Tbh I’ve got all my money on skimage.metrics.structural_similarity at the moment. If that doesn’t quite fit then I’m not sure but I’ll figure it out.

Looked it up. Looks promising. Good luck. I’ve got a bunch of old image classifier data sets that I’ve downloaded from web. I could use them for testing. Happy to test early code if helpful.

1 Like

You are changing the fastbook module. May I ask are the changes expected to work right off the bat? What I mean is, once I make the changes to, Do I need to re-install or restart the kernel?

It doesn’t seem to work right off the bat, after making changes to

Thej, I don’t think this change ever made it into

Also if anyone still cares about getting this working as intended, you should look at the totalEstimatedMatches in the original search result returned to see how many results you have in total and page through them 150 at a time. Also look at the nextOffset param in conjunction with offset to avoid duplicate results.

I think you have misunderstood my question. My question was about how to see the changes I make to Cause, from utils import * didn’t work.