Untitled

 avatar
unknown
plain_text
2 years ago
1.2 kB
4
Indexable
from google_images_download import google_images_download
import os

def download_images(query, limit=100, output_directory='smiling_images'):
    response = google_images_download.googleimagesdownload()

    arguments = {
        "keywords": query,
        "limit": limit,
        "print_urls": True,
        "output_directory": output_directory,
    }

    try:
        paths = response.download(arguments)
        download_path = paths[0][query]
        
        # Rename the downloaded images to the desired format
        rename_images(download_path)
        
        print(f"\nDownloaded {len(download_path)} images of {query} to {output_directory}")
    except Exception as e:
        print(f"\nError: {str(e)}")

def rename_images(directory):
    count = 1
    for filename in os.listdir(directory):
        if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
            new_filename = f"group1_{count}.jpeg"
            os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
            count += 1

if __name__ == "__main__":
    # Change the query to customize the search
    download_images("smiling person", limit=1000)
Editor is loading...
Leave a Comment