Norod78

Google Images Search via Google Custom Search API

Jan 28th, 2021 (edited)
1,604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. from 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
  8. gis = GoogleImagesSearch('Your GCS_DEVELOPER_KEY goes here', 'Your GCS_CX goes here')
  9.  
  10. search_q = 'Disney princess'
  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': 'face',
  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:
  31. try:
  32.     gis.search(search_params=_search_params, path_to_dir='./gimages/', custom_image_name=search_q.replace(' ', '_'))
  33. except:
  34.     print("Okay, bye now")
  35.  
Add Comment
Please, Sign In to add comment