I implemented what seems to be a Windows snipping tool (a program that saves the area selected by drag and drop as an image) in Python. It may be a rather aggressive implementation, but please forgive me.
I uploaded the program running on Youtube, so please take a look. https://www.youtube.com/watch?v=e2zePSUGwaA
When I created the program of "I tried playing a typing game with Python" posted last time, it was captured on the screen. When I was looking for a way to get the pixel coordinates of the area I wanted to get intuitively and dynamically </ font>, I thought that a format like the Windows snipping tool was the best, so let's implement it in Python. I thought.
OS : Windows10 Python Version : 3.5.3
PyQt5, Pillow, OpenCV
main.py
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import tkinter as tk
from PIL import ImageGrab, Image
import matplotlib.pyplot as plt
import numpy as np
import cv2
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
self.setGeometry(0,0,screen_width, screen_height)
self.setWindowTitle("")
self.setWindowOpacity(0.3)
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.begin = QtCore.QPoint()
self.end = QtCore.QPoint()
print("Capture the screen...")
self.show()
def paintEvent(self, event):
qp = QtGui.QPainter(self)
qp.setPen(QtGui.QPen(QtGui.QColor("black"), 3))
qp.setBrush(QtGui.QColor(128, 128, 255, 128))
qp.drawRect(QtCore.QRect(self.begin, self.end))
def mousePressEvent(self, event):
self.begin = event.pos()
self.end = self.begin
self.update()
def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.end = event.pos()
self.close()
x1 = min(self.begin.x(), self.end.x())
y1 = min(self.begin.y(), self.end.y())
x2 = max(self.begin.x(), self.end.x())
y2 = max(self.begin.y(), self.end.y())
img = ImageGrab.grab(bbox=(x1,y1,x2,y2))
img.save("capture.png ")
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
cv2.imshow('Captured Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWidget()
window.show()
app.aboutToQuit.connect(app.deleteLater)
sys.exit(app.exec_())
ImageGrab
of `` `Pillow``` and save it as an image.I was able to implement what seems to be a Windows snipping tool in Python.
Recommended Posts