pyqt5 implements labeling boxes for pictures on QLabel

Table of contents

1. Demand

Use QLabel to display pictures, now you need to drag and drop the label box on the picture.

2. Realize

Mainly refer to the blog "PyQt5 uses QPainter to draw rectangles in QLabel" , modify two of the codes to make the picture fidelity, and always keep consistent with the mouse when dragging the rectangle.

Directly upload the source code, where the image address img = QPixmap('./14.png')needs to be replaced by yourself.

from PyQt5.QtWidgets import QWidget, QApplication, QLabel
from PyQt5.QtCore import QRect, Qt
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen
import cv2
import sys
 
class MyLabel(QLabel):
    x0 = 0
    y0 = 0
    x1 = 0
    y1 = 0
    flag = False
 
    #鼠标点击事件
    def mousePressEvent(self,event):
        self.flag = True
        self.x0 = event.x()
        self.y0 = event.y()
 
    #鼠标释放事件
    def mouseReleaseEvent(self,event):
        self.flag = False
 
    #鼠标移动事件
    def mouseMoveEvent(self,event):
        if self.flag:
            self.x1 = event.x()
            self.y1 = event.y()
            self.update()
 
    #绘制事件
    def paintEvent(self, event):
        super().paintEvent(event)
        # rect =QRect(self.x0, self.y0, abs(self.x1-self.x0), abs(self.y1-self.y0))
        # 这里换成任意方向的矩形框
        # 标注框的左上角和右下角坐标
        x0 = min(self.x0, self.x1)
        y0 = min(self.y0, self.y1)
        x1 = max(self.x0, self.x1)
        y1 = max(self.y0, self.y1)
        width = x1 - x0
        height = y1 - y0

        # 矩形
        rect =QRect(x0, y0, width, height)
        painter = QPainter(self)
        painter.setPen(QPen(Qt.red,2,Qt.SolidLine))
        painter.drawRect(rect)
 
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        self.resize(675, 500)
        self.move(100,50)
        self.setWindowTitle('在label中绘制矩形')
        self.lb = MyLabel(self) #重定义的label
        # 这里直接读取图片
        img = QPixmap('./14.png') 
        # 往显示视频的Label里 显示QImage
        self.lb.setPixmap(img)
        self.lb.setCursor(Qt.CrossCursor)#图片可以绘制
        self.show()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    x = Example()
    sys.exit(app.exec_())

Guess you like

Origin blog.csdn.net/qq_30841655/article/details/127733444