PySide(PyQt)在图像上画线

1、按鼠标左键任意画线

python 复制代码
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、画直线

python 复制代码
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())
相关推荐
王哈哈^_^12 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
苏三有春4 天前
PyQt5实战——UTF-8编码器UI页面设计以及按钮连接(五)
python·pyqt
充值内卷5 天前
PyQt入门指南三十六 QInputDialog输入对话框组件
开发语言·python·pyqt
yava_free7 天前
用PyQt 5 开发的雷达基数据可视化软件
python·pyqt
充值内卷8 天前
PyQt入门指南三十五 QAction动作组件
linux·python·pyqt
星寂樱易李10 天前
python--pyQt 单选按钮控件 -QRadioButton
开发语言·python·pyqt
彭祥.12 天前
点云标注工具开发记录(四)之点云根据类别展示与加速渲染
pyqt·opengl
love_songming13 天前
Pyside6 布局管理器(3)--- QGridLayout的使用
开发语言·python·pyqt·pyside6
痛&快乐着14 天前
python-PyQt项目实战案例:制作一个视频播放器
python·pyqt
goomind15 天前
YOLOv8实战野生动物识别
人工智能·python·yolo·目标检测·pyqt