PyQt-如何在图像上覆盖矩形

Sen*_*gul 1 python label pyqt rectangles draw

在PyQt标签上显示图像后,我想在显示的图像上方绘制一个矩形。请注意,我并不是说“绘制”是指用户在图像上“绘制”矩形的地方,而是我只想在图像顶部创建一个矩形。我有matplotlib轴的等效代码,但我不确定如何在PyQt中执行相同的操作。

# Create Figure/Axes Instance
figure,axes = matplotlib.pyplot.subplots()
axes.imshow(imageRGB)

# Draw Rectangle
axes.add_patch(matplotlib.patches.Rectangle((50,50),100,100,fill=False,edgecolor='red'))
Run Code Online (Sandbox Code Playgroud)

Sen*_*gul 6

# convert image file into pixmap
self.pixmap_image = QtGui.QPixmap(self.filename)

# create painter instance with pixmap
self.painterInstance = QtGui.QPainter(self.pixmap_image)

# set rectangle color and thickness
self.penRectangle = QtGui.QPen(QtCore.Qt.red)
self.penRectangle.setWidth(3)

# draw rectangle on painter
self.painterInstance.setPen(self.penRectangle)
self.painterInstance.drawRect(xPos,yPos,xLen,yLen)

# set pixmap onto the label widget
self.ui.label_imageDisplay.setPixmap(self.pixmap_image)
self.ui.label_imageDisplay.show()
Run Code Online (Sandbox Code Playgroud)