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 天前
pyside6的历史发展、Qt 介绍、PyQt 和 pyside6对比
qt·pyqt
开心-开心急了3 天前
PySide6 实现win10 手动与自动切换主题 借助系统托盘
pyqt·1024程序员节·pyside
mortimer5 天前
用 PySide6 打造可视化 ASS 字幕样式编辑器:从需求到实现
python·ffmpeg·pyqt
没有感情的robot7 天前
pyqt实现简易中英文翻译工具
pyqt
mortimer9 天前
用PySide6 构建一个响应式视频剪辑工具:多线程与信号机制实战
python·ffmpeg·pyqt
深兰科技11 天前
深兰科技法务大模型亮相,推动律所文书处理智能化
人工智能·scrapy·beautifulsoup·scikit-learn·pyqt·fastapi·深兰科技
叶子丶苏13 天前
第八节_PySide6基本窗口控件_按钮类控件(QAbstractButton)
python·pyqt
龙腾AI白云13 天前
大模型-7种大模型微调方法 上
scrapy·scikit-learn·pyqt
开心-开心急了15 天前
PySide6 使用搜索引擎搜索 多类实现 更新1次
python·pyqt·pyside
herbal_medicine15 天前
【测试】123456789
pyqt