|
import matplotlib |
|
|
|
matplotlib.use('Agg') |
|
import matplotlib.pyplot as plt |
|
|
|
plt.ioff() |
|
import os |
|
|
|
os.environ['TERM'] = 'dumb' |
|
import requests |
|
from serpapi import GoogleSearch |
|
from PIL import Image |
|
from io import BytesIO |
|
import os |
|
import argparse |
|
|
|
|
|
def download_image(text, file, save_dir='.'): |
|
|
|
os.makedirs(save_dir, exist_ok=True) |
|
|
|
|
|
params = { |
|
"engine": "google_images", |
|
"q": text, |
|
"api_key": os.getenv("SERPAPI_API_KEY") |
|
} |
|
|
|
|
|
search = GoogleSearch(params) |
|
results = search.get_dict() |
|
|
|
|
|
if "images_results" in results and len(results["images_results"]) > 0: |
|
|
|
image_url = results["images_results"][0]["original"] |
|
|
|
|
|
response = requests.get(image_url) |
|
if response.status_code == 200: |
|
|
|
img = Image.open(BytesIO(response.content)).convert("RGB") |
|
|
|
|
|
filepath = os.path.join(save_dir, file) |
|
|
|
|
|
img.save(filepath) |
|
print(f"Image downloaded and saved as {filepath}") |
|
return filepath |
|
else: |
|
print(f"Failed to download image for text: {text}") |
|
return None |
|
else: |
|
print(f"No image results found for text: {text}") |
|
return None |
|
|
|
|
|
def main(): |
|
|
|
assert os.getenv("SERPAPI_API_KEY"), "Please set the SERPAPI_API_KEY environment variable" |
|
|
|
parser = argparse.ArgumentParser(description="Download one image from the web based on a search text") |
|
parser.add_argument("--text", "--prompt", "--query", type=str, required=True, help="The text to search for") |
|
parser.add_argument("--output", "--file", type=str, help="The file name to save the image to") |
|
args = parser.parse_args() |
|
download_image(text=args.text, file=args.output) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|