Last active
April 9, 2021 18:05
-
-
Save atulkumar2/ce7a0506ff6607c325c4cf6f862dfc8d to your computer and use it in GitHub Desktop.
Take screenshot of the whole screen at regular intervals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################### | |
# Takes screenshot at defined interval and saves at a defined path | |
# Minimize the program window for proper screenshot | |
# Install package pyautogui in a separate conda environment | |
# Prefer to use anaconda.org to create environment | |
# Created on 2021-04-09 | |
############################################################################### | |
import os | |
import time | |
from datetime import datetime, timedelta | |
import pyautogui | |
# ------------------------------------------------------------------------------ | |
# USER DEFINED VARIABLES | |
# EDIT AS PER YOUR NEED | |
# Use double backslash in folder path if on windows | |
TIME_TO_WAIT = 5 | |
SAVE_TO_FOLDER = 'C:\\Temp\\1' | |
TOTAL_RUN_TIME_MINUTE = 720 | |
# ------------------------------------------------------------------------------ | |
class StringOps(object): | |
@classmethod | |
def null_or_empty(cls, input: str) -> bool: | |
if input is None or len(input) == 0: | |
return True | |
else: | |
return False | |
@classmethod | |
def not_null_or_empty(cls, input: str) -> bool: | |
return (not StringOps.null_or_empty(input=input)) | |
class TimeOps(object): | |
def __init__(self) -> None: | |
super().__init__() | |
@classmethod | |
def time_stamp_now(cls): | |
now = datetime.now() | |
date_time = now.strftime('%Y%m%d-%H%M%S') | |
return date_time | |
@classmethod | |
def date_stamp_current(cls): | |
now = datetime.now() | |
date_str = now.strftime('%Y%m%d') | |
return date_str | |
@classmethod | |
def time_stamp_after_minutes(cls, minutes): | |
now = datetime.now() | |
then = now + timedelta(minutes=minutes) | |
date_time_str = then.strftime('%Y%m%d-%H%M%S') | |
return date_time_str | |
class FileOps(object): | |
@classmethod | |
def create_folder_if_missing(cls, folder_path: str): | |
if (StringOps.null_or_empty(folder_path)): | |
raise ValueError('Null or empty folder path') | |
if os.path.exists(folder_path): | |
print('Folder <{}> exists.'.format(folder_path)) | |
else: | |
try: | |
os.mkdir(folder_path) | |
except Exception as ex: | |
print('Folder <{}> could not be created. <{}>'.format( | |
folder_path, str(ex))) | |
if __name__ == "__main__": | |
print('') | |
FileOps.create_folder_if_missing(SAVE_TO_FOLDER) | |
if not os.path.exists(SAVE_TO_FOLDER): | |
print('Despite best effort, Path <{}> could not be created. Please create on your own or give proper path.'.format( | |
SAVE_TO_FOLDER)) | |
exit(1) | |
print('Program would save screenshots at {}'.format(SAVE_TO_FOLDER)) | |
print('Program would finish at {}'.format( | |
TimeOps.time_stamp_after_minutes(minutes=TOTAL_RUN_TIME_MINUTE))) | |
print('Press CTRL+C for early stop\n\n') | |
start_time_str = TimeOps.time_stamp_now() | |
start_time = datetime.now() | |
failure_tracker = 0 | |
count = 0 | |
try: | |
while True: | |
count += 1 | |
current_time = datetime.now() | |
minute_from_start = round( | |
(current_time - start_time).seconds / 60, ndigits=2) | |
if minute_from_start > TOTAL_RUN_TIME_MINUTE: | |
print('\n\n') | |
print('{}: Exceeded Total run time. Stopping now after {} minutes.'.upper( | |
).format(count, minute_from_start)) | |
break | |
current_time_str = TimeOps.time_stamp_now() | |
filename = os.path.join( | |
SAVE_TO_FOLDER, '{}.png'.format(current_time_str)) | |
try: | |
myScreenshot = pyautogui.screenshot() | |
myScreenshot.save(filename) | |
if failure_tracker > 0: | |
failure_tracker = 0 | |
except Exception as ex: | |
failure_tracker += 1 | |
print('{}: Problem Saving <{}>. <{}>'.format( | |
count, filename, str(ex))) | |
if failure_tracker > 10: | |
print('{}: Problem Saving <{}> for 10 times at stretch. Exiting'.format( | |
count, filename, str(ex))) | |
break | |
if os.path.exists(filename): | |
print('{}: Saved {}. Started at {}. {}/{} minutes. Use CTRL+C to stop.'.format( | |
count, filename, start_time_str, minute_from_start, TOTAL_RUN_TIME_MINUTE)) | |
else: | |
print('{}: Could not Save {}. Started at {}. {}/{} minutes. Use CTRL+C to stop.'.format( | |
count, filename, start_time_str, minute_from_start, TOTAL_RUN_TIME_MINUTE)) | |
time.sleep(TIME_TO_WAIT) | |
except KeyboardInterrupt as ex: | |
print('\n\n') | |
print('Received Keyboard interrupt.'.upper()) | |
print( | |
'Thanks for using this program. I am exiting now as you wanted.'.upper()) | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment