I tried to implement what seems to be a Windows snipping tool in Python

Overview

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

Motivation

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.

environment

OS : Windows10 Python Version : 3.5.3

Main libraries used

PyQt5, Pillow, OpenCV

Program behavior

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_())


  1. Using `` `PyQt5```, place a GUI with a transparent background so that it covers the entire screen.
  2. Using the mouse-related event listener, acquire the pixel coordinates according to the drag on the GUI, and draw a rectangle based on it.
  3. At the time of dropping, after erasing the GUI, capture the selected area using ImageGrab of `` `Pillow``` and save it as an image.
  4. Display the acquired image using ```OpenCV` ``.

application

  1. Combine with a program that starts from screen capture.
  2. Do something with the acquired coordinates.

Summary

I was able to implement what seems to be a Windows snipping tool in Python.

Recommended Posts

I tried to implement what seems to be a Windows snipping tool in Python
I tried to implement a pseudo pachislot in Python
I tried to implement a one-dimensional cellular automaton in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried to implement PPO in Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to implement a card game of playing cards in Python
I want to easily implement a timeout in python
I tried to implement GA (genetic algorithm) in Python
I tried "How to get a method decorated in Python"
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I tried to implement blackjack of card game in Python
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
What seems to be a template of the standard input part of the competition pro in python3
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I created a password tool in Python.
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to develop a Formatter that outputs Python logs in JSON
A note I looked up to make a command line tool in Python
I want to create a window in Python
I tried playing a typing game in Python
[Memo] I tried a pivot table in Python
I tried adding a Python3 module in C
I tried to implement merge sort in Python with as few lines as possible
What I was addicted to when creating a web application in a windows environment
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I tried to explain what a Python generator is for as easily as possible.
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I tried to implement Minesweeper on terminal with python
I want to write in Python! (2) Let's write a test
I tried to implement a recommendation system (content-based filtering)
I want to randomly sample a file in Python
I tried to implement an artificial perceptron with python
I want to work with a robot in python.
I tried to automatically generate a password with Python3
I tried to summarize how to use pandas in python
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
I tried to implement StarGAN (1)
I tried to implement a volume moving average with Quantx
I tried to implement a basic Recurrent Neural Network model
I tried to create API list.csv in Python from swagger.yaml
Processing of python3 that seems to be usable in paiza
[Markov chain] I tried to read a quote into Python.
I tried "a program that removes duplicate statements in Python"
How to implement Python EXE for Windows in Docker container
I created a class in Python and tried duck typing
Create a tool to check scraping rules (robots.txt) in Python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I want to be able to run Python in VS Code