|
| 1 | +import mss |
| 2 | +import time |
| 3 | +import os |
| 4 | +import pyscreenshot as grabShot |
| 5 | +import pygetwindow as grabWinShot |
| 6 | +import ctypes |
| 7 | +from PIL import Image |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +def saveFile(save_Dir, shots_length, img, file_format): |
| 11 | + timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S') |
| 12 | + filename = f'screen_shot{shots_length}.{file_format}' |
| 13 | + filepath = os.path.join(save_Dir, filename) |
| 14 | + img.save(filepath, file_format.upper()) |
| 15 | + print(f'Screenshot saved successfully! (Shots remaining: {shots_length - 1})') |
| 16 | + print(f'Saved image: {filepath}') |
| 17 | + |
| 18 | +def full_Screen_Capture(monitorNum, file_format): |
| 19 | + save_Dir = input("Define Location to Save Images : ") |
| 20 | + shots_length = int(input("How many Number of Screen Shots : ")) |
| 21 | + |
| 22 | + if not os.path.exists(save_Dir): |
| 23 | + os.makedirs(save_Dir) |
| 24 | + |
| 25 | + if shots_length <= 0 : |
| 26 | + print("Invalid Length of ScreenShots") |
| 27 | + return |
| 28 | + |
| 29 | + while(shots_length): |
| 30 | + monitor = monitors[monitorNum] |
| 31 | + screenshot = sct.grab(monitor) |
| 32 | + img = Image.frombytes("RGB", (screenshot.width, screenshot.height), screenshot.rgb) |
| 33 | + |
| 34 | + saveFile(save_Dir, shots_length, img, file_format) |
| 35 | + shots_length -= 1 |
| 36 | + |
| 37 | +def bring_window_to_foreground(window): |
| 38 | + try: |
| 39 | + ctypes.windll.user32.SetForegroundWindow(window._hWnd) |
| 40 | + time.sleep(2) # Wait a bit for the window to come to the foreground |
| 41 | + return True |
| 42 | + except Exception as e: |
| 43 | + print(f'Error bringing window to foreground: {e}') |
| 44 | + return False |
| 45 | + |
| 46 | +def grabWindowShot(window_Title, file_format): |
| 47 | + save_Dir = input("Define Location to Save Images: ") |
| 48 | + shots_length = int(input("How many Number of Screen Shots: ")) |
| 49 | + |
| 50 | + if shots_length <= 0: |
| 51 | + print("Invalid Length of ScreenShots") |
| 52 | + return |
| 53 | + |
| 54 | + while shots_length: |
| 55 | + windows = grabWinShot.getWindowsWithTitle(window_Title) |
| 56 | + if not windows: |
| 57 | + print(f'Window with Title "{window_Title}" not found') |
| 58 | + return |
| 59 | + |
| 60 | + if len(windows) > 1: |
| 61 | + print(f'Multiple Windows with Title "{window_Title}" found') |
| 62 | + for i, window in enumerate(windows): |
| 63 | + print(f'{i+1}. {window.title}') |
| 64 | + try: |
| 65 | + window_choice = int(input("Enter the number of the desired window: ")) - 1 |
| 66 | + if window_choice < 0 or window_choice >= len(windows): |
| 67 | + print("Invalid window selection") |
| 68 | + continue |
| 69 | + window = windows[window_choice] |
| 70 | + except ValueError: |
| 71 | + print("Invalid input. Please enter a number: ") |
| 72 | + continue |
| 73 | + else: |
| 74 | + window = windows[0] |
| 75 | + |
| 76 | + try: |
| 77 | + print(f'Attempting to bring window "{window.title}" to the foreground') |
| 78 | + if not window.isMinimized: |
| 79 | + if bring_window_to_foreground(window): |
| 80 | + print(f'Window "{window.title}" is in the foreground. Capturing screenshot...') |
| 81 | + bbox = (window.left, window.top, window.right, window.bottom) |
| 82 | + print(f'Window bounds: {bbox}') |
| 83 | + |
| 84 | + try: |
| 85 | + img = grabShot.grab(bbox=bbox, backend="mss") |
| 86 | + saveFile(save_Dir, shots_length, img, file_format) |
| 87 | + except Exception as capture_error: |
| 88 | + print(f'Error capturing screenshot with bbox {bbox}. Exception: {capture_error}') |
| 89 | + return |
| 90 | + |
| 91 | + else: |
| 92 | + print(f'Failed to bring window "{window.title}" to foreground.') |
| 93 | + return |
| 94 | + else: |
| 95 | + print(f'The window "{window.title}" is minimized. Please restore it and try again.') |
| 96 | + return |
| 97 | + |
| 98 | + except Exception as e: |
| 99 | + print(f'Error activating window: "{window_Title}". Exception: {e}') |
| 100 | + return |
| 101 | + |
| 102 | + shots_length -= 1 |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + print("******* Welcome to Screenshot Taking Application *******") |
| 107 | + execute = True |
| 108 | + while(execute): |
| 109 | + types = ['Full-Screen-Screenshot', 'Specific-Window-Screenshot'] |
| 110 | + |
| 111 | + for index, val in enumerate(types): |
| 112 | + print(f'{index+1} - {val}') |
| 113 | + sel_type = int(input("Please Select an Option (1 or 2) : ")) |
| 114 | + |
| 115 | + try: |
| 116 | + if(sel_type == 1): |
| 117 | + with mss.mss() as sct: |
| 118 | + monitors = sct.monitors |
| 119 | + if (len(monitors) > 1): |
| 120 | + chooseMonitor = int(input("Please Enter a Monitor No : ")) |
| 121 | + file_form = input("File format : ") |
| 122 | + full_Screen_Capture(chooseMonitor, file_form) |
| 123 | + elif(len(monitors) == 1): |
| 124 | + file_form = input("File format : ") |
| 125 | + full_Screen_Capture(1, file_form) |
| 126 | + |
| 127 | + elif(sel_type == 2): |
| 128 | + window_title = input("Window Title : ") |
| 129 | + file_form = input("File Format : ") |
| 130 | + grabWindowShot(window_title, file_form) |
| 131 | + else: |
| 132 | + print("Invalid Option Selected") |
| 133 | + |
| 134 | + execution = input("exit or continue : ") |
| 135 | + if(execution.lower() == 'exit'): |
| 136 | + execute = False |
| 137 | + elif(execution.lower() == 'continue'): |
| 138 | + execute = True |
| 139 | + else: |
| 140 | + print("Invalid Option Selected") |
| 141 | + |
| 142 | + except KeyboardInterrupt: |
| 143 | + print("Application Crash - Execute Again") |
| 144 | + exit() |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
0 commit comments