Last active
September 23, 2021 05:24
-
-
Save FernandoCelmer/cc0d569f0261de9340d6ab3d6bb966c5 to your computer and use it in GitHub Desktop.
Script Python para capturar a tela a cada 5 minutos e salvar a captura em uma pasta pré-determinada.
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
# -*- coding: utf-8 -*- | |
""" | |
ScreenShot Script | |
""" | |
import os | |
import time | |
import datetime | |
import cv2 | |
import pyautogui | |
import numpy | |
from PIL import Image | |
from screeninfo import get_monitors | |
from dotenv import load_dotenv | |
class Schreenshot(): | |
""" | |
ScreenShot Script | |
""" | |
def __init__(self): | |
load_dotenv() | |
url = os.environ.get("PATH_SCREENSHOT") | |
date = str(datetime.datetime.now().strftime("%Y-%m-%d")) | |
self.path = f"{url}{date}/" | |
def create_dir(self): | |
""" | |
Create a new directory with the name referring to the current day | |
""" | |
if not os.path.isdir(self.path): | |
os.mkdir(self.path) | |
def create_image(self, img, image_name, screem): | |
""" | |
Split a screenshot dynamically by screen size | |
""" | |
if screem.is_primary: | |
img.crop((0, 0, screem.width, screem.height)).save( | |
self.path+image_name.replace("[]", "_1") | |
) | |
else: | |
img.crop((0, screem.height, screem.width, screem.height+768)).save( | |
self.path+image_name.replace("[]", "_2") | |
) | |
def run(self): | |
""" | |
Run screen capture script | |
""" | |
self.create_dir() | |
loop = True | |
while loop: | |
now = datetime.datetime.now() | |
image = pyautogui.screenshot() | |
image_name = "Screenshot_"+ str(now.strftime("%Y-%m-%d-%H-%M-%S"))+"[].png" | |
monitors = get_monitors() | |
print(image_name) | |
image = cv2.cvtColor( | |
numpy.array(image), | |
cv2.COLOR_RGB2BGR | |
) | |
cv2.imwrite(self.path+image_name, image) | |
img = Image.open(self.path+image_name) | |
for screem in monitors: | |
self.create_image( | |
img=img, | |
image_name=image_name, | |
screem=screem | |
) | |
os.remove(self.path+image_name) | |
# Wait 5 minutes | |
time.sleep(300) | |
if __name__ == "__main__": | |
script = Schreenshot() | |
script.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment