PySide(PyQt)在图像上画线

发布于:2024-06-29 ⋅ 阅读:(62) ⋅ 点赞:(0)

1、按鼠标左键任意画线

import sys
from PySide6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PySide6.QtGui import QPainter, QPixmap, QMouseEvent, QColor, QPen
from PySide6.QtCore import Qt, QPoint


class PaintLabel(QLabel):
    def __init__(self, parent=None):
        super(PaintLabel, self).__init__(parent)
        self.setFixedSize(500, 500)
        self.pixmap = QPixmap(500, 500)  # 创建图像
        self.pixmap.fill(QColor('#000000'))  # 设为黑色
        self.setPixmap(self.pixmap)  # 设置图像
        self.drawing = False
        self.last_point = QPoint()

    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.last_point = event.position()

    def mouseMoveEvent(self, event: QMouseEvent):
        if event.buttons() & Qt.LeftButton and self.drawing:
            painter = QPainter(self.pixmap)
            pen = QPen(QColor('red'))
            pen.setWidth(3)
            painter.setPen(pen)
            painter.drawLine(self.last_point, event.position())
            self.last_point = event.position()
            self.setPixmap(self.pixmap)

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = False


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.label = PaintLabel()

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        # layout.addWidget(save_button)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec())

2、画直线 

 

import sys
from PySide6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PySide6.QtGui import QPainter, QPixmap, QMouseEvent, QColor, QPen
from PySide6.QtCore import Qt, QPoint, QLine


class PaintLabel(QLabel):

    def __init__(self, parent=None):
        super(PaintLabel, self).__init__(parent)

        self.line = QLine()
        self.setFixedSize(500, 500)
        self.pixmap = QPixmap(500, 500)  # 创建图像
        self.pixmap.fill(QColor('#000000'))  # 设为黑色
        self.setPixmap(self.pixmap)  # 设置图像
        self.drawing = False         # 是否绘画中
        self.start_pos = QPoint()    # 起点
        self.end_pos = QPoint()      # 终点

    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.start_pos = event.position().toPoint()

    def mouseMoveEvent(self, event: QMouseEvent):
        if event.buttons() & Qt.LeftButton and self.drawing:
            self.end_pos = event.position().toPoint()
            self.line = QLine(self.start_pos, self.end_pos)
            self.update()

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = False
            self.update()

    def paintEvent(self, event):
        super().paintEvent(event)  # 继承绘画事件
        if self.drawing:  # 如果正在绘画中
            painter = QPainter(self)  # 就不必刷新和写入,只是在当前的label上展示一下新增的线条而已
            pen = QPen(QColor(0, 255, 0), 2, Qt.CustomDashLine)   # 虚线
            pen.setDashPattern([1, 4, 5, 4])  # 设置自定义虚线模式
            painter.setPen(pen)
            painter.drawLine(self.line)
            painter.end()
        else:
            painter = QPainter(self.pixmap)  # 如果是松开鼠标的最后结果,就在图像上画
            painter.setPen(QPen(QColor(255, 0, 0, 255), 2, Qt.SolidLine))
            painter.drawLine(self.line)
            self.line = QLine()   # 画完后舍弃

            self.setPixmap(self.pixmap)    # 刷新显示


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.label = PaintLabel()

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec())