/ tuesdaytooling

Tuesday Tooling - Google Image Search With Python

Screenshot-from-2018-07-31-11-27-31
We must spend days of our lives searching for images on Google. That picture of a duck on a pond, the cat pic where it is stalking like a ninja.

But sometimes we want to automate looking for pics, and then we turn to Python and typically the requests library and a little Python-Fu!

But what if we could do it a little better?

So what is it?

Goolge-Images-Download, a Python library to search and download images from Hardik Vasa

How can I install it?

Using the pip package manager.

pip3 install google_images_download

So how can I use it?

From the Terminal / CLI

googleimagesdownload -k "Google Image Search" -l 1

The arguments

-k Keyword to search for.
-l Limit, how many images to download

Your download will be saved to a folder called downloads inside the directory where the command is run. But this can be changed.

Via Python

from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()
absolute_image_paths = response.download({"keywords":"Google Image Search","limit":1})

For Python we create an object called response which is used to query Google with our search term.
Then we create an object called absolute_image_paths which will provide the arguments, just as we did in the terminal.

So how can I do something useful with it?

I sat down this morning and wrote a little script in Mu v1.0.0 (you should really download that and have a play) that asks the user for their search term and then runs a Google image search using that keyword(s).

Here is the code

First the imports and setting up our response object.

from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()

Then we create a variable called run_again later used to capture if the user wants to use the script again.

run_again = "Y"

Next up we use a while loop to check if the user wants to search, first time is always yes. We check to see if the run_again variable has a "Y" or "y" using "or" logic.

while run_again == "y" or run_again == "Y":

So now we create a new variable that will store the search term / keyword that the user wants to search for. This is saved as a string.

    search = input("Please enter the keyword(s) to search for. Please note, multiple keywords should be separated  with a , : \n")

We then search for the keyword, again limiting the response to just one image. Note that there is a new argument, no_directory which will save the image to the downloads folder created by this code, but the image is not saved to it's own directory, for example if we search for penguin without this new argument, the file is saved to ./downloads/penguin but with this new argument it is saved to ./downloads

    absolute_image_paths = response.download({"keywords":search,"limit":1,"no_directory":"1"})

Still inside the while loop we ask the user if they want to search for a new keyword, if so the loop repeats, if not, we print "Bye" to the REPL and the script ends.

    run_again = input("Perform another search? Y or N:")
print("Bye!")

When run the code should look like this.
TuesdayTooling-GoogleImageSearch

Have fun!