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