本文主要是介绍QGraphicsView 实现图片裁剪功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本教程实现了图片显示、滚轮放大缩小图片、鼠标中键移动图片、画框裁剪图片功能。
界面程序
from PyQt5.QtCore import QSize, Qt, QRect
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QApplication, QDialog, QGridLayout, QPushButton, QSpacerItem, QSizePolicytry:from .graphics import GraphicsView, GraphicsPixmapItem
except:from graphics import GraphicsView, GraphicsPixmapItemclass Form(QDialog):def __init__(self):super(Form, self).__init__()self.resize(1024, 768)self.picture = r'test.png'self.init_ui()# 视图背景颜色self.graphicsView.setBackgroundBrush(QColor(28, 31, 34))self.graphicsView.save_signal.connect(self.pushButton_save.setEnabled)self.pushButton_cut.clicked.connect(self.pushButton_cut_clicked)self.pushButton_save.clicked.connect(self.pushButton_save_clicked)# image_item = GraphicsPolygonItem()# image_item.setFlag(QGraphicsItem.ItemIsMovable)# self.scene.addItem(image_item)def init_ui(self):self.gridLayout = QGridLayout(self)self.pushButton_cut = QPushButton('cut', self)self.pushButton_cut.setCheckable(True)self.pushButton_cut.setMaximumSize(QSize(100, 16777215))self.gridLayout.addWidget(self.pushButton_cut, 0, 0, 1, 1)self.pushButton_save = QPushButton('save', self)self.pushButton_save.setEnabled(False)self.gridLayout.addWidget(self.pushButton_save, 1, 0, 1, 1)spacerItem = QSpacerItem(20, 549, QSizePolicy.Minimum, QSizePolicy.Expanding)self.gridLayout.addItem(spacerItem, 2, 0, 1, 1)self.graphicsView = GraphicsView(self.picture, self)self.graphicsView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.graphicsView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.gridLayout.addWidget(self.graphicsView, 0, 1, 3, 1)def pushButton_cut_clicked(self):if self.graphicsView.image_item.is_start_cut:self.graphicsView.image_item.is_start_cut = Falseself.graphicsView.image_item.setCursor(Qt.ArrowCursor) # 箭头光标else:self.graphicsView.image_item.is_start_cut = Trueself.graphicsView.image_item.setCursor(Qt.CrossCursor) # 十字光标def pushButton_save_clicked(self):rect = QRect(self.graphicsView.image_item.start_point.toPoint(),self.graphicsView.image_item.end_point.toPoint())new_pixmap = self.graphicsView.image_item.pixmap().copy(rect)new_pixmap.save(r'test.png')if __name__ == '__main__':import sysapp = QApplication(sys.argv)form = Form()form.show()app.exec_()
重构QGraphicsView和QGraphicsPixmapItem
from PyQt5.QtCore import QRectF, Qt, pyqtSignal
from PyQt5.QtGui import QColor, QPixmap, QPen
from PyQt5.QtWidgets import QGraphicsView, QGraphicsPixmapItem, QGraphicsScene, QGraphicsItemclass GraphicsView(QGraphicsView):save_signal = pyqtSignal(bool)def __init__(self, picture, parent=None):super(GraphicsView, self).__init__(parent)# 设置放大缩小时跟随鼠标self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)self.scene = QGraphicsScene()self.setScene(self.scene)self.image_item = GraphicsPixmapItem(QPixmap(picture))self.image_item.setFlag(QGraphicsItem.ItemIsMovable)self.scene.addItem(self.image_item)size = self.image_item.pixmap().size()# 调整图片在中间self.image_item.setPos(-size.width() / 2, -size.height() / 2)self.scale(0.1, 0.1)def wheelEvent(self, event):'''滚轮事件'''zoomInFactor = 1.25zoomOutFactor = 1 / zoomInFactorif event.angleDelta().y() > 0:zoomFactor = zoomInFactorelse:zoomFactor = zoomOutFactorself.scale(zoomFactor, zoomFactor)def mouseReleaseEvent(self, event):'''鼠标释放事件'''# print(self.image_item.is_finish_cut, self.image_item.is_start_cut)if self.image_item.is_finish_cut:self.save_signal.emit(True)else:self.save_signal.emit(False)class GraphicsPixmapItem(QGraphicsPixmapItem):save_signal = pyqtSignal(bool)def __init__(self, picture, parent=None):super(GraphicsPixmapItem, self).__init__(parent)self.setPixmap(picture)self.is_start_cut = Falseself.current_point = Noneself.is_finish_cut = Falsedef mouseMoveEvent(self, event):'''鼠标移动事件'''self.current_point = event.pos()if not self.is_start_cut or self.is_midbutton:self.moveBy(self.current_point.x() - self.start_point.x(),self.current_point.y() - self.start_point.y())self.is_finish_cut = Falseself.update()def mousePressEvent(self, event):'''鼠标按压事件'''super(GraphicsPixmapItem, self).mousePressEvent(event)self.start_point = event.pos()self.current_point = Noneself.is_finish_cut = Falseif event.button() == Qt.MidButton:self.is_midbutton = Trueself.update()else:self.is_midbutton = Falseself.update()def paint(self, painter, QStyleOptionGraphicsItem, QWidget):super(GraphicsPixmapItem, self).paint(painter, QStyleOptionGraphicsItem, QWidget)if self.is_start_cut and not self.is_midbutton:# print(self.start_point, self.current_point)pen = QPen(Qt.DashLine)pen.setColor(QColor(0, 150, 0, 70))pen.setWidth(3)painter.setPen(pen)painter.setBrush(QColor(0, 0, 255, 70))if not self.current_point:returnpainter.drawRect(QRectF(self.start_point, self.current_point))self.end_point = self.current_pointself.is_finish_cut = True
成果
这篇关于QGraphicsView 实现图片裁剪功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!