diff --git a/Screenshoter/README.md b/Screenshoter/README.md new file mode 100644 index 000000000..e7bf95cec --- /dev/null +++ b/Screenshoter/README.md @@ -0,0 +1,8 @@ +# Screenshoter + +Take screenshots with two clicks on screen (top-left and bottom-right). + +* Press Up key : For defining particular window to take screenshots on. +* Press Click : To set top-left coordinates. +* Press Click : To set bottom-right coordinates. +* Press Down Key : For capturing and storing image. diff --git a/Screenshoter/reqirements.txt b/Screenshoter/reqirements.txt new file mode 100644 index 000000000..d6404804a --- /dev/null +++ b/Screenshoter/reqirements.txt @@ -0,0 +1,4 @@ +pynput==1.7.1 +numpy==1.18.1 +PyAutoGUI==0.9.52 +opencv-python==4.4.0.44 \ No newline at end of file diff --git a/Screenshoter/screenshooter.py b/Screenshoter/screenshooter.py new file mode 100644 index 000000000..e415abad5 --- /dev/null +++ b/Screenshoter/screenshooter.py @@ -0,0 +1,101 @@ +import cv2 +import numpy as np +import pynput.mouse as ms +import pynput.keyboard as kb +from pynput.keyboard import Key, Controller +import pyautogui + +keyboard = Controller() + + +class TwoClicksScreenShot: + clickCount = 0 + pCords = [0, 0, 0, 0] + defined = False + screenShot = None + filename = "screenshot" + + @staticmethod + def area_select(): + + print('Click twice to define Screenshot area') + + def on_click(x, y, button, pressed): + + if pressed: + + if TwoClicksScreenShot.clickCount == 0: + print('First point ({0}, {1})'.format(x, y)) + TwoClicksScreenShot.pCords[0] = x + TwoClicksScreenShot.pCords[1] = y + elif TwoClicksScreenShot.clickCount == 1: + print('Second point ({0}, {1})'.format(x, y)) + TwoClicksScreenShot.pCords[2] = x - TwoClicksScreenShot.pCords[0] + TwoClicksScreenShot.pCords[3] = y - TwoClicksScreenShot.pCords[1] + TwoClicksScreenShot.defined = True + print('') + TwoClicksScreenShot.clickCount = 0 + return False + TwoClicksScreenShot.clickCount += 1 + + with ms.Listener(on_click=on_click) as listener: + listener.join() + + @staticmethod + def keypress(): + + print('Press UP key to define Screenshot Area') + + def on_release(key): + if key == Key.up: + print('Pressed\n') + TwoClicksScreenShot.area_select() + if TwoClicksScreenShot.capture(): + return False + else: + print('Define points properly!! Press UP again to define') + return True + + with kb.Listener(on_release=on_release) as listener: + listener.join() + + @staticmethod + def start_typing(): + + print('Press DOWN key to capture') + + def on_release(key): + if key == Key.down: + print('Pressed\n') + TwoClicksScreenShot.output() + return False + + with kb.Listener(on_release=on_release) as listener: + listener.join() + + @staticmethod + def capture(): + if TwoClicksScreenShot.pCords[2] <= 0 or TwoClicksScreenShot.pCords[3] <= 0: + return False + + if TwoClicksScreenShot.defined: + TwoClicksScreenShot.screenShot = pyautogui.screenshot(region=(TwoClicksScreenShot.pCords[0], + TwoClicksScreenShot.pCords[1], + TwoClicksScreenShot.pCords[2], + TwoClicksScreenShot.pCords[3])) + TwoClicksScreenShot.screenShot = cv2.cvtColor(np.array(TwoClicksScreenShot.screenShot), cv2.COLOR_BGR2RGB) + return True + + @staticmethod + def output(): + cv2.imwrite(TwoClicksScreenShot.filename + ".png", TwoClicksScreenShot.screenShot) + print("Screenshot saved as " + TwoClicksScreenShot.filename + ".png") + + +def start(): + TwoClicksScreenShot.keypress() + TwoClicksScreenShot.start_typing() + + +if __name__ == '__main__': + start()