[英]Selecting Screen 2 from Snipping_Tool in Python
I am using a similar tool than SnippingTool from: https://github.com/harupy/snipping-tool .我正在使用与 SnippingTool 类似的工具,来自: 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.我有 2 个屏幕,我想知道如何从屏幕 2 抓取图片,因为我的代码仅从屏幕 1 捕获图片,即使我在屏幕 2 中设置了几何图形。
My main screen (#1) is the bottom screen and my secondary screen (#2) is the top one.我的主屏幕 (#1) 是底部屏幕,我的辅助屏幕 (#2) 是顶部屏幕。
The configuration for the screens are multiple and extended displays.屏幕的配置是多个和扩展的显示。 Both resolutions are 1920 x 1080.
两种分辨率均为 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.我想知道如何 select 不同的屏幕以及是否可以同时从两个屏幕捕获。
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_())
The problem is that event.pos() are relative to the widget's coordinate system, not the screen.问题是 event.pos() 是相对于小部件的坐标系,而不是屏幕。 One possible solution is to convert it to global coordinates using mapToGlobal:
一种可能的解决方案是使用 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.注意:我还没有测试过
img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
部分,所以我不知道ImageGrab
库是否支持多个 windows。
On the other hand, I see it unnecessary to use third libraries since Qt can take screenshots:另一方面,我认为没有必要使用第三个库,因为 Qt 可以截屏:
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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.