screenshot.py 1023 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. import argparse
  3. import pyautogui
  4. import time
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("-p", "--path", help="absolute path to store screenshot.", default=r"./images")
  7. parser.add_argument("-t", "--type", help="h (in hour) or m (in minutes) or s (in seconds)", default='h')
  8. parser.add_argument("-f", "--frequency", help="frequency for taking screenshot per h/m/s.", default=1, type=int)
  9. args = parser.parse_args()
  10. sec = 0.
  11. if args.type == 'h':
  12. sec = 60 * 60 / args.frequency
  13. elif args.type == 'm':
  14. sec = 60 / args.frequency
  15. if sec < 1.:
  16. sec = 1.
  17. if os.path.isdir(args.path) != True:
  18. os.mkdir(args.path)
  19. try:
  20. while True:
  21. t = time.localtime()
  22. current_time = time.strftime("%H_%M_%S", t)
  23. file = current_time + ".jpg"
  24. image = pyautogui.screenshot(os.path.join(args.path,file))
  25. print(f"{file} saved successfully.\n")
  26. time.sleep(sec)
  27. except KeyboardInterrupt:
  28. print("End of script by user interrupt")