PyQt - 如何在图像上叠加矩形

3
在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'))
1个回答

10
# 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()

1
在上面的代码中,我们可以使用 self.penRectangle.setWidth(3) 来改变边框厚度。 - biendltb
1
@Radha Saraf,我知道已经过了好几年,但是我现在卡在这个点上了。有没有办法让这个矩形可选/可点击,以便为该矩形分配一些名称。提前致谢。 - iamkk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接