· 5 years ago · Jan 28, 2021, 04:30 PM
1from google_images_search import GoogleImagesSearch
2
3# Google-Images-Search
4# https://pypi.org/project/Google-Images-Search/
5
6# you can provide API key and CX using arguments,
7# or you can set environment variables: GCS_DEVELOPER_KEY, GCS_CX
8gis = GoogleImagesSearch('Your GCS_DEVELOPER_KEY goes here', 'Your GCS_CX goes here')
9
10search_q = 'Rick and Morty'
11
12# define search params:
13_search_params = {
14 'q': search_q,
15 'num': 200, #usually a higher number will not yield more good results, better run again with a different search_q
16 'safe': 'off',
17 'fileType': 'jpg',
18 #'imgType': 'clipart|face|lineart|news|photo',
19 #'imgType': 'face', #useful for FFHQ transfer-learning
20 'imgType': 'photo',
21 #'imgSize': ' "['imgSizeUndefined', 'HUGE', 'ICON', 'LARGE', 'MEDIUM', 'SMALL', 'XLARGE', 'XXLARGE']"',
22 'imgSize': 'XLARGE',
23 #'imgDominantColor': 'black|blue|brown|gray|green|pink|purple|teal|white|yellow',
24 #'rights': 'cc_publicdomain|cc_attribute|cc_sharealike|cc_noncommercial|cc_nonderived'
25}
26
27# this will only search for images:
28#gis.search(search_params=_search_params)
29
30# this will search and download:
31try:
32 gis.search(search_params=_search_params, path_to_dir='./gimages/', custom_image_name=search_q.replace(' ', '_'))
33except:
34 print("Okay, bye now")
35