2017-03-06 64 views
1

在PyQt標籤上顯示圖像後,我想在顯示的圖像上繪製一個矩形。請注意,我並不是指用戶在圖像上「繪製」矩形的地方繪製的,而是指我只是想在圖像上創建一個矩形。我有matplotlib軸的等效代碼,但我不知道如何在PyQt中做同樣的事情。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')) 

回答

4
# 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.penRedBorder.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()