Skip to content

Instantly share code, notes, and snippets.

@guiszk
Created December 18, 2021 03:30
Show Gist options
  • Save guiszk/9993a590862aacc273ec9e8df2501db6 to your computer and use it in GitHub Desktop.
Save guiszk/9993a590862aacc273ec9e8df2501db6 to your computer and use it in GitHub Desktop.
#code was found here: https://github.com/arrrlo/Google-Images-Search
#i'm saving it as a gist for easier access
from google_images_search import GoogleImagesSearch
import sys
if(len(sys.argv) != 3):
print(f"{sys.argv[0]} <search term> <number>")
sys.exit()
# you can provide API key and CX using arguments,
# or you can set environment variables: GCS_DEVELOPER_KEY, GCS_CX
PROJECT_KEY = "PROJECT_KEY"
PROJECT_CX = "PROJECT_CX"
gis = GoogleImagesSearch(PROJECT_KEY, PROJECT_CX)
searchterm = sys.argv[1]
num = int(sys.argv[2])
#'safe': 'high|medium|off',
#'fileType': 'jpg|gif|png',
#'imgType': 'clipart|face|lineart|news|photo',
#'imgSize': 'HUGE|ICON|LARGE|MEDIUM|SMALL|XLARGE|XXLARGE',
#'imgDominantColor': 'black|blue|brown|gray|green|orange|pink|purple|red|teal|white|yellow',
#'imgColorType': 'color|gray|mono|trans',
#'rights': 'cc_publicdomain|cc_attribute|cc_sharealike|cc_noncommercial|cc_nonderived'
# define search params:
_search_params = {
'q': searchterm,
'num': num,
'safe': 'medium',
'fileType': 'jpg',
'imgType': 'photo',
'imgSize': 'MEDIUM',
'imgDominantColor': 'black',
'imgColorType': 'color',
'rights': 'cc_publicdomain'
}
# this will only search for images:
#gis.search(search_params=_search_params)
# this will search and download:
gis.search(search_params=_search_params, path_to_dir='./'+'_'.join(searchterm.split(' ')))
# this will search, download and resize:
#gis.search(search_params=_search_params, path_to_dir='/path/', width=500, height=500)
# search first, then download and resize afterwards:
"""
gis.search(search_params=_search_params)
for image in gis.results():
image.download('/path/')
image.resize(500, 500)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment