Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Screenshoter/README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions Screenshoter/reqirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pynput==1.7.1
numpy==1.18.1
PyAutoGUI==0.9.52
opencv-python==4.4.0.44
101 changes: 101 additions & 0 deletions Screenshoter/screenshooter.py
Original file line number Diff line number Diff line change
@@ -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()