gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:36 25 4
gpt4 key购买 nike

在 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'))

最佳答案

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

关于python - PyQt - 如何在图像上叠加矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com