I am using the following code to download images from Google-image-search API using python. However, no matter what 'search_parameters' I use I get grayscale images. I need to get color images:
Python 3.11.4
PyCharm 2023.2 (Community Edition)
I tried the following code:
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'
}
gis.search(search_params=_search_params, path_to_dir=spath)
get_image("cat")
UPDATE: if I use "cat meme" string in get_image("cat meme")
, it doesn't return any image.
I was able to figure this out! I had only added one website to my search engine i.e. image.google.com/*
I removed that website and set my search engine to "search the entire web", this solved both the grey scale problem and also the "no results" problem that occurred with some "search keywords".