1

I am using a similar tool than SnippingTool from: https://github.com/harupy/snipping-tool.
I have 2 screens and I would like to know how to grab a picture from screen 2 because my code only capture picture from screen 1, even when I set a geometry in screen 2.
My main screen (#1) is the bottom screen and my secondary screen (#2) is the top one.
The configuration for the screens are multiple and extended displays. Both resolutions are 1920 x 1080.
I would like to know how to select the different screens as well as if it is possible to capture from both screens together.
Below follows my code:

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import tkinter as tk
from PIL import ImageGrab
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) # Geometry for Screen 1 - bottom)
        self.setGeometry(0, -1080, screen_width, screen_height) # Geometry for Screen 2 - top)

        self.setWindowTitle(' ')
        self.begin = QtCore.QPoint()
        self.end = QtCore.QPoint()
        self.setWindowOpacity(0.3)
        QtWidgets.QApplication.setOverrideCursor(
            QtGui.QCursor(QtCore.Qt.CrossCursor)
        )
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        print('Capture the screen...')
        self.show()
        print(screen_width, screen_height)

    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()
        print(self.begin)
        self.end = self.begin
        print(self.end)
        self.update()

    def mouseMoveEvent(self, event):
        self.end = event.pos()
        self.update()

    def mouseReleaseEvent(self, event):
        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())

        # (x1, y1, x2, y2) = (268, -749, 941, -631)

        print(x1, y1, x2, y2)
        
        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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

2

The problem is that event.pos() are relative to the widget's coordinate system, not the screen. One possible solution is to convert it to global coordinates using mapToGlobal:

def mouseReleaseEvent(self, event):
    super().mouseReleaseEvent(event)
    self.close()
    self.end = event.pos()

    rect = QtCore.QRect(
        self.mapToGlobal(self.begin), self.mapToGlobal(self.end)
    ).normalized()
    x1, y1, x2, y2 = rect.getCoords()

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

Note: I haven't tested the img = ImageGrab.grab(bbox=(x1, y1, x2, y2)) part so I don't know if the ImageGrab library supports multiple windows.


Qt Solution:

On the other hand, I see it unnecessary to use third libraries since Qt can take screenshots:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle(" ")
        self.begin = QtCore.QPoint()
        self.end = QtCore.QPoint()

        self.setWindowOpacity(0.3)

        QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        print("Capture the screen...")

        self.showFullScreen()

    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):
        super().mousePressEvent(event)
        self.begin = event.pos()
        self.end = self.begin
        self.update()

    def mouseMoveEvent(self, event):
        super().mouseMoveEvent(event)
        self.end = event.pos()
        self.update()

    def mouseReleaseEvent(self, event):
        self.close()
        QtCore.QTimer.singleShot(1000, self.screenshot)

    def screenshot(self):
        print("screenshot")
        screen = QtGui.QGuiApplication.primaryScreen()
        window = self.windowHandle()
        if window is not None:
            screen = window.screen()
        if screen is None:
            print("failed")
            return

        original_pixmap = screen.grabWindow(0)
        output_pixmap = original_pixmap.copy(
            QtCore.QRect(self.begin, self.end).normalized()
        )
        output_pixmap.save("capture.png")

        self.label = QtWidgets.QLabel(pixmap=output_pixmap)
        self.label.show()
        app.setQuitOnLastWindowClosed(True)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyWidget()
    window.show()
    app.aboutToQuit.connect(app.deleteLater)
    app.setQuitOnLastWindowClosed(False)
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Hi eyllanesc, thanks for your response. I implemented your solution, however when I try to capture from screen 2 (top) the picture is completed black. From screen 1 (bottom) it is working. Do you think that other functions like "def __init__(self):" should be changed to global coordinates? – Henrique Gustavo Argentieri Apr 29 '21 at 03:40
  • @HenriqueGustavoArgentieri ImageGrab.grab supports multiple screens? – eyllanesc Apr 29 '21 at 03:52
  • @HenriqueGustavoArgentieri I can propose an alternative but I need feedback from you: 1) how big are your screens? 2) what do you get when you run `print(x1, y1, x2, y2)` in your first window and what do you get in the second window? I'm asking because I don't have 2 screens so my answer will be based on the information you provide. – eyllanesc Apr 29 '21 at 03:57
  • @HenriqueGustavoArgentieri Try my Qt solution – eyllanesc Apr 29 '21 at 04:16
  • Really thanks eyllanesc. I used your Qt solution and the capture screen is working for screen 1, actually the command "self.showFullScreen()" enables only screen 1. Anyway I swaped my screens because I need to capture from the bigger screen. I implemented your solution because it is really clean and requires only PyQt5 library. Thanks a lot! – Henrique Gustavo Argentieri May 02 '21 at 12:22