我有LabelTool类,可以在图像上创建一个矩形,这是可行的,但是我不知道如何将所选区域另存为新图像,我还想知道该矩形的高度和宽度,我需要此参数用于为另一个项目创建txt文件。
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QScrollArea
from PyQt5.uic import loadUi
class LabelTool(QtWidgets.QLabel):
def __init__(self, parent=None):
QtWidgets.QLabel.__init__(self,parent)
self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
position = QtCore.QPoint(event.pos())
if self.selection.isVisible():
print(self.upper_left.manhattanLength())
if (self.upper_left - position).manhattanLength() < 20:
self.mode = "drag_upper_left"
elif (self.lower_right - position).manhattanLength() < 20:
self.mode = "drag_lower_right"
else:
self.selection.hide()
else:
self.upper_left = position
self.lower_right = position
self.mode = "drag_lower_right"
self.selection.show()
def mouseMoveEvent(self, event):
'''
Mouse moved. If selection is visible, drag it according to drag mode.
'''
if self.selection.isVisible():
# visible selection
if self.mode is "drag_lower_right":
self.lower_right = QtCore.QPoint(event.pos())
print(str(self.lower_right))
elif self.mode is "drag_upper_left":
self.upper_left = QtCore.QPoint(event.pos())
print(str(self.upper_left))
# update geometry
self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())
这是主窗口,由qgraphicsview小部件和一些按钮组成
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi('GUI/gui.ui', self)
kpe_window = LabelTool()
pixmap = QtGui.QPixmap("image.JPG")
kpe_window.setPixmap(pixmap)
kpe_window.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setFocusProxy(self.grafic)
kpe_window.setFocus(True)
lay = QtWidgets.QVBoxLayout(self.grafic)
lay.addWidget(kpe_window)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
这是ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GUI</class>
<widget class="QMainWindow" name="GUI">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>770</width>
<height>527</height>
</rect>
</property>
<property name="windowTitle">
<string>MetaxaReborn</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="2">
<widget class="QGraphicsView" name="grafic"/>
</item>
<item row="0" column="3">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QPushButton" name="loadFile_btn">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset>
<normaloff>icons/file.png</normaloff>icons/file.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="loadFolder_btn">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset>
<normaloff>icons/folder.png</normaloff>icons/folder.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="clear_btn">
<property name="text">
<string>clear</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>