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())
相关推荐
大学生毕业题目1 天前
毕业项目推荐:83-基于yolov8/yolov5/yolo11的农作物杂草检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·杂草识别
凯子坚持 c4 天前
当Python遇见高德:基于PyQt与JS API构建桌面三维地形图应用实战
javascript·python·pyqt·高德地图
Goona_5 天前
pyqt+Python证件号智能校验工具
pyqt
大学生毕业题目6 天前
毕业项目推荐:52-基于yolov8/yolov5/yolo11的红绿灯检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·红绿灯检测
大学生毕业题目7 天前
毕业项目推荐:64-基于yolov8/yolov5/yolo11的蝴蝶种类检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·蝴蝶检测
大学生毕业题目7 天前
毕业项目推荐:51-基于yolov8/yolov5/yolo11的反光衣检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·反光衣检测
深兰科技8 天前
柳州市委常委、统战部部长,副市长潘展东率队首访深兰科技集团新总部,共探 AI 赋能制造大市与东盟合作新局
人工智能·beautifulsoup·numpy·pyqt·matplotlib·pygame·深兰科技
傻啦嘿哟11 天前
用PyQt快速搭建桌面应用:从零到实战的实用指南
pyqt
大学生毕业题目12 天前
毕业项目推荐:28-基于yolov8/yolov5/yolo11的电塔危险物品检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·cnn·pyqt·电塔·危险物品
Ciel_752113 天前
AmazeVault 核心功能分析,认证、安全和关键的功能
python·pyqt·pip