Google Image Fetcher is a Python library that allows you to fetch images from Google Search using the Custom Search JSON API.
from google_image_fetcher.google_image_fetcher import GoogleImageFetcher

# Create a GoogleImageFetcher instance
fetcher = GoogleImageFetcher()

# Define the search query
query = "cats"

# Fetch and save images
fetcher.fetch_images(query, save_folder="images")
google_image_fetcher/__init__.py
PB2204/google_image_fetcher
# google_image_fetcher/google_image_fetcher.py

import os
import requests
from typing import Optional
from dotenv import load_dotenv

load_dotenv()


class GoogleImageFetcher:
    API_KEY = os.getenv("API_KEY")
    CX_ID = os.getenv("CX_ID")
    BASE_URL = "https://www.googleapis.com/customsearch/v1"

    def fetch_images(self, query: str, save_folder: Optional[str] = "images"):
        """
        Fetch images from Google based on a query and save them to a specified folder.

        Args:
            query (str): The search query for images.
            save_folder (str, optional): The folder where images will be saved. Defaults to "images".
        """
        params = {
            "q": query,
            "searchType": "image",
            "key": self.API_KEY,
            "cx": self.CX_ID,
        }

        response = requests.get(self.BASE_URL, params=params)

        if response.status_code == 200:
            data = response.json()
            results = data.get("items", [])

            if not os.path.exists(save_folder):
                os.makedirs(save_folder)

            for i, item in enumerate(results):
                img_url = item["link"]
                img_extension = img_url.split(".")[-1]
                img_filename = f"{i + 1}.{img_extension}"
                img_path = os.path.join(save_folder, img_filename)

                try:
                    img_data = requests.get(img_url).content
                    with open(img_path, "wb") as img_file:
                        img_file.write(img_data)

                    print(f"Image {i + 1}: {img_url}")

                except Exception as e:
                    print(f"Error downloading image {i + 1}: {e}")

        else:
            print(f"Error: {response.status_code}")

google_image_fetcher/google_image_fetcher.py
PB2204/google_image_fetcher
from setuptools import setup, find_packages
import codecs
import os

here = os.path.abspath(os.path.dirname(__file__))

with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
    long_description = "\n" + fh.read()

VERSION = "2.0.0"
DESCRIPTION = "Fetch & Download Images From Google"
LONG_DESCRIPTION = "Google Image Fetcher is a Python library that allows you to fetch images from Google Search using the Custom Search JSON API."

# Setting up
setup(
    name="google_image_fetcher",
    version=VERSION,
    author="PB2204 (Pabitra Banerjee)",
    author_email="<rockstarpabitra2204@gmail.com>",
    description=DESCRIPTION,
    long_description_content_type="text/markdown",
    long_description=long_description,
    packages=find_packages(),
    install_requires=["requests", "python-dotenv"],
    keywords=["python", "image", "fetch", "web scrapping", "image scrapping"],
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "Operating System :: Unix",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
    ],
)

setup.py
PB2204/google_image_fetcher