run.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. if __name__ != '__main__': raise Exception("Do not import me!")
  3. from argparse import ArgumentParser
  4. from google_images_download import google_images_download #importing the library
  5. parser = ArgumentParser()
  6. parser.add_argument("classnames",
  7. help="file containing a class name in each line")
  8. parser.add_argument("--output_directory", "-o", default="download",
  9. help="output folder")
  10. parser.add_argument("--limit", "-l", default=60, type=int,
  11. help="number of images to dowload")
  12. def main(args):
  13. with open(args.classnames, "r") as f:
  14. names = f.readlines()
  15. query_names = [name.strip().partition(".")[-1].replace("_", " ").lower() for name in names if not name.startswith("#")]
  16. # query_names = [name.strip() for name in names if not name.startswith("#")]
  17. response = google_images_download.googleimagesdownload() #class instantiation
  18. print(f"Found {len(query_names)} query names")
  19. paths = response.download(dict(
  20. keywords=",".join(query_names),
  21. limit=args.limit,
  22. print_urls=False,
  23. size=">800*600",
  24. output_directory=args.output_directory,
  25. chromedriver="chromedriver"
  26. )) #passing the arguments to the function
  27. # print(paths) #printing absolute paths of the downloaded images
  28. main(parser.parse_args())