Untitled
unknown
plain_text
2 years ago
1.6 kB
8
Indexable
from google_images_download import google_images_download
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
def download_images(query, limit=100, output_directory='smiling_images'):
options = Options()
options.headless = True # Run Firefox in headless mode
browser = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver", options=options)
response = google_images_download.googleimagesdownload()
arguments = {
"keywords": query,
"limit": limit,
"print_urls": True,
"output_directory": output_directory,
"chromedriver": "/usr/local/bin/geckodriver", # Specify the path to geckodriver here
}
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)}")
finally:
browser.quit()
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