Make python scripts to download images for you.

Sharmila S
Apr 6 · 3 min read

In this article, I’ll be showing you how we could write python scripts to search and download images from google.

Be it any machine learning project, the most boring part of it would be data collection. But what if we can automate the process?

Let’s attempt to search for Persian cat images and save them to a directory.

Firstly, Install the Google-Images-Search package using pip or conda from your command line.

$ pip install Google-Images-Search

In a new python file or jupyter notebook, import the module.

from google_images_search import GoogleImagesSearch

The next step is to get an API key and a CX key.

Step 1: Create a new project in https://console.developers.google.com

Step 2: Visit Custom Search API, and select your project from the navbar and click Enable to enable the API for your project.

Step 3: Go to Credentials and generate a new API key

Step 4: Visit https://cse.google.com/cse/all, Click Add under Edit Search Engine.
Provide `www.google.com` in Sites to Search, and a name of your choice under Name.
Choose the recently created Search Engine and copy the Search engine ID which is our CX code.

You can provide the keys either using Environment variables or arguments.

key = os.environ.get('GCS_DEVELOPER_KEY') 
cx = os.environ.get('GCS_CX')
gis = GoogleImagesSearch(key, cx)

Now, we are going to define the search parameters for our query.

_search_params = {
'q' : 'Persian Cat',
'num' : 10,
'safe' : 'high',
'fileType': 'jpg|png',
'imgType' : 'photo',
}

q represents the query string.

num specifies the number of images.

safe indicates whether the safe search is high/medium/off.

imgtype takes values like ‘clipart’, ‘photo’, ‘lineart’ etc. Since we need real photos of cat, we specified the type as ‘photo’.

Let’s try calling the API by passing our search parameter, and download each image to the /images/persian directory.
We are resizing the image after downloading it but it is optional.

gis.search(search_params=_search_params,custom_image_name='persian')for image in gis.results():
image.download('/images/persian') # download location
image.resize(500, 500) # resize the image

Let’s see whether we got our images downloaded.

os.listdir('/images/persian')

By adjusting the num attribute, we can download as many images as we want. To mention, the API limit is 10 images per request. Hence, if you specify 50 images, API will be called 5 times to fulfil the requests.

Github repository link 👉 https://github.com/SharmilaS22/medium-google-search-api

Happy learning!

featurepreneur

Features for everyone!