How to save the image from Google-Image-Search API in python with a specific file name?

127 Views Asked by At

I am using google-image-search API without any problem. I want to get the name of the file that is downloaded (or potentially give it a different name). I need to pass the name of the file to another python script.

If important:
OS: Ubuntu 22.04
Python 3.11.4

Here is the code I'm using:

def get_image(image_keyword=""):

    from dotenv import load_dotenv
    import os
    from google_images_search import GoogleImagesSearch
    if not os.path.exists('images/'):
        os.mkdir('images/')
    spath = 'images/'
    if os.path.exists(".env"):
        load_dotenv()
    else:
        print(".env file missing, please create one with your API and CX")
    dk = os.environ.get('DEVELOPER_KEY')
    cx = os.environ.get('CX')
    gis = GoogleImagesSearch(dk, cx)
    _search_params = {
        'q': image_keyword,
        'num': 1,
        'imageColorType': 'color',
        'fileType': 'jpg',
        'safe': 'high'
        }
    gis.search(search_params=_search_params, path_to_dir=spath)


get_image("")

Is there a argument in the search method that saves the file with a given name? If not, can I extract the name of the file from the search method?

0

There are 0 best solutions below