Skip to content

Instantly share code, notes, and snippets.

@aeruhxi
Last active July 13, 2017 13:46
Show Gist options
  • Save aeruhxi/10bbcb7e24b95c6f258f272d2df197e2 to your computer and use it in GitHub Desktop.
Save aeruhxi/10bbcb7e24b95c6f258f272d2df197e2 to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt, QRect, QPoint, QEvent, QBuffer, QByteArray, QIODevice
from PyQt5.QtGui import QPalette, QPainter, QPixmap, QPen, QBrush, QColor, QClipboard
import signal
class ScreenshotWindow(QMainWindow):
def __init__(self):
super().__init__()
self.lastPoint = None
self.drawing = False
self.currentPoint = None
self.croppedImage = None
# Screen and it size
self.screen = app.primaryScreen()
self.screenSize = self.screen.size()
# Initialize window
self._initializeWindow()
self.show()
def _initializeWindow(self):
self.setGeometry(0, 0, self.screenSize.width(),
self.screenSize.height())
self.setWindowFlags(Qt.FramelessWindowHint)
self.pixmap = self.screen.grabWindow(0)
self.showFullScreen()
def paintEvent(self, event):
painter = QPainter(self)
# Draw image
image = self.pixmap.toImage()
painter.drawImage(0, 0, image)
# Draw black overlay
painter.fillRect(QRect(0, 0, self.screenSize.width(),
self.screenSize.height()),
QColor(0, 0, 0, 130))
painter.setPen(QPen(Qt.red, 1, Qt.DashLine))
if (self.lastPoint != None):
# Copy image within userdrawn rectangle
self.croppedImage = image.copy(self.lastPoint.x(), self.lastPoint.y(),
self.currentPoint.x() - self.lastPoint.x() - 1,
self.currentPoint.y() - self.lastPoint.y() - 1)
# Draw rectangle
painter.drawRect(
QRect(self.lastPoint.x(), self.lastPoint.y(),
self.currentPoint.x() - self.lastPoint.x(),
self.currentPoint.y() - self.lastPoint.y()))
# Draw cropped image
painter.drawImage(self.lastPoint.x() + 1,
self.lastPoint.y() + 1, self.croppedImage)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.lastPoint = event.pos()
self.drawing = True
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) and self.drawing:
self.currentPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if (event.button() & Qt.LeftButton) and self.drawing:
self.drawing = False
def keyPressEvent(self, event):
keyPressed = event.key()
if keyPressed == Qt.Key_Escape:
QApplication.exit()
elif keyPressed == Qt.Key_C and (event.modifiers() & Qt.ControlModifier):
if self.croppedImage != None:
# data = QByteArray()
# buffer = QBuffer(data)
# buffer.open(QIODevice.WriteOnly)
# self.croppedImage.save(buffer, 'PNG')
# b64Data = data.toBase64()
# plainData = base64.b64decode(b64Data)
# pyperclip.copy(plainData)
clipboard = QApplication.clipboard()
clipboard.setImage(self.croppedImage)
print("Copied to clipboard")
# Copy real data to clipboard instead of just pointer
clipboard = QApplication.clipboard()
event = QEvent(QEvent.Clipboard)
QApplication.sendEvent(clipboard, event)
QApplication.exit()
if __name__ == '__main__':
# Enable CTRL+C
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QApplication(sys.argv)
screenWidget = ScreenshotWindow()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment