1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| import io import sys
import threading
import win32con from PIL import ImageGrab from PyQt5.QtWidgets import QApplication, QWidget, QDialog from PyQt5.QtCore import Qt, qAbs, QRect from PyQt5.QtGui import QPen, QPainter, QColor, QGuiApplication from loguru import logger import win32clipboard as w
from run import constants
class CaptureScreen(QDialog): beginPosition = None endPosition = None fullScreenImage = None captureImage = None isMousePressLeft = None painter = QPainter()
def __init__(self): super(QWidget, self).__init__() self.initWindow() self.captureFullScreen()
def initWindow(self): self.setMouseTracking(True) self.setCursor(Qt.CrossCursor) self.setWindowFlag(Qt.FramelessWindowHint) self.setWindowState(Qt.WindowFullScreen)
def captureFullScreen(self): self.fullScreenImage = QGuiApplication.primaryScreen().grabWindow(QApplication.desktop().winId())
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.beginPosition = event.pos() self.isMousePressLeft = True if event.button() == Qt.RightButton: if self.captureImage is not None: self.captureImage = None self.paintBackgroundImage() self.update() else: self.close()
def mouseMoveEvent(self, event): if self.isMousePressLeft is True: self.endPosition = event.pos() self.update()
def mouseReleaseEvent(self, event): self.endPosition = event.pos() self.isMousePressLeft = False
def mouseDoubleClickEvent(self, event): if self.captureImage is not None: self.saveImage() self.close()
def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: constants.select_screenshot_flag = False logger.debug("Screenshot cancel.") self.close() if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return: if self.captureImage is not None: self.saveImage() self.close()
def paintBackgroundImage(self): shadowColor = QColor(0, 0, 0, 100) self.painter.drawPixmap(0, 0, self.fullScreenImage) self.painter.fillRect(self.fullScreenImage.rect(), shadowColor)
def paintEvent(self, event): self.painter.begin(self) self.paintBackgroundImage() penColor = QColor(30, 144, 245) self.painter.setPen(QPen(penColor, 1, Qt.SolidLine, Qt.RoundCap)) if self.isMousePressLeft is True: pickRect = self.getRectangle(self.beginPosition, self.endPosition) self.captureImage = self.fullScreenImage.copy(pickRect) self.painter.drawPixmap(pickRect.topLeft(), self.captureImage) self.painter.drawRect(pickRect) self.painter.end()
def getRectangle(self, beginPoint, endPoint): pickRectWidth = int(qAbs(beginPoint.x() - endPoint.x())) pickRectHeight = int(qAbs(beginPoint.y() - endPoint.y())) pickRectTop = beginPoint.x() if beginPoint.x() < endPoint.x() else endPoint.x() pickRectLeft = beginPoint.y() if beginPoint.y() < endPoint.y() else endPoint.y() pickRect = QRect(pickRectTop, pickRectLeft, pickRectWidth, pickRectHeight) if pickRectWidth == 0: pickRect.setWidth(2) if pickRectHeight == 0: pickRect.setHeight(2)
return pickRect
def saveImage(self): if constants.select_screenshot_flag: save_img_threading_obj = threading.Thread( target=screenshot_img_save, args=(self,)) save_img_threading_obj.start() return True else: logger.error("save screenshot ing, please wait.") return False
@logger.catch def screenshot_img_save(self): """
:param self: :return: """ try: if self.beginPosition.x() < self.endPosition.x(): x1 = self.beginPosition.x() x2 = self.endPosition.x() else: x2 = self.beginPosition.x() + 2 x1 = self.endPosition.x() if self.beginPosition.y() < self.endPosition.y(): y1 = self.beginPosition.y() y2 = self.endPosition.y() else: y2 = self.beginPosition.y() + 2 y1 = self.endPosition.y() box = [x1, y1, x2, y2] screenshot = ImageGrab.grab(box)
img_bytes = io.BytesIO() screenshot.convert("RGB").save(img_bytes, "BMP") data = img_bytes.getvalue()[14:] img_bytes.seek(0)
w.OpenClipboard() w.EmptyClipboard()
w.SetClipboardData(win32con.CF_DIB, data)
w.CloseClipboard() logger.success(f"Screenshot success, image select region: {self.beginPosition} : {self.endPosition}") constants.select_screenshot_flag = False except Exception as e: logger.error(f"Unknown error, detail: {e}") constants.select_screenshot_flag = False
@logger.catch def screenshot_select(): """
:return: """ windows = CaptureScreen() windows.show() windows.exec()
|