Python 如何强制屏幕剪大小比。PyQt5
我想修改来自的屏幕剪贴代码,以便每个屏幕剪贴的比率为3 x 2。(稍后我将另存为600 x 400像素图像) 我不知道如何动态修改Python 如何强制屏幕剪大小比。PyQt5,python,pyqt5,Python,Pyqt5,我想修改来自的屏幕剪贴代码,以便每个屏幕剪贴的比率为3 x 2。(稍后我将另存为600 x 400像素图像) 我不知道如何动态修改self.end,以便用户以3 x 2的比率单击和拖动。鼠标位置将定义x坐标,y坐标将为int(x*2/3) 有什么建议吗?我保证我一直在研究这一点,我只是不能“破解代码”,只修改y坐标的self.end 代码如下: import sys import PyQt5 from PyQt5 import QtWidgets, QtCore, QtGui import tk
self.end
,以便用户以3 x 2的比率单击和拖动。鼠标位置将定义x
坐标,y
坐标将为int(x*2/3)
有什么建议吗?我保证我一直在研究这一点,我只是不能“破解代码”,只修改y
坐标的self.end
代码如下:
import sys
import PyQt5
from PyQt5 import QtWidgets, QtCore, QtGui
import tkinter as tk
from PIL import ImageGrab
import numpy as np
import cv2 # package is officially called opencv-python
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.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()
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)) ##### This seems like the place I should modify. #########
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.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_())
您不需要“更改y坐标”,只需要使用正确的参数来创建矩形。
初始化QRect有多种方法,您可以使用两点,另一种方法(更常见的方法)是使用原点坐标和矩形大小
一旦知道了宽度,就可以计算高度,如果终点的y高于起点,则将其设为负值
请注意,通过这种方式,您可以得到一个“负”矩形(负宽度,“右”边实际上位于左侧,高度/底部相同),因此通常更好地使用它,这也允许您获得用于抓屏的矩形的正确坐标
class MyWidget(QtWidgets.QWidget):
# ...
def getRect(self):
# a commodity function that always return a correctly sized
# rectangle, with normalized coordinates
width = self.end.x() - self.begin.x()
height = abs(width * 2 / 3)
if self.end.y() < self.begin.y():
height *= -1
return QtCore.QRect(self.begin.x(), self.begin.y(),
width, height).normalized()
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(self.getRect())
def mouseReleaseEvent(self, event):
self.close()
rect = self.getRect()
img = ImageGrab.grab(bbox=(
rect.topLeft().x(),
rect.topLeft().y(),
rect.bottomRight().x(),
rect.bottomRight().y()
))
# ...
class MyWidget(QtWidgets.QWidget):
# ...
def showEvent(self, event):
if not event.spontaneous():
# delay the geometry on the "next" cycle of the Qt event loop;
# this should take care of positioning issues for systems that
# try to move newly created windows on their own
QtCore.QTimer.singleShot(0, self.resetPos)
def resetPos(self):
rect = QtCore.QRect()
# create a rectangle that is the sum of the geometries of all available
# screens; the |= operator acts as `rect = rect.united(screen.geometry())`
for screen in QtWidgets.QApplication.screens():
rect |= screen.geometry()
self.setGeometry(rect)