2017-10-11 188 views
0

以下行是在我的腳本: 顯示在一個標籤的圖像,而不保存它

 
    from PyQt5 import QtGui, QtWidgets, QtCore 
    from PyQt5.QtGui import QIcon, QPixmap 
    from PyQt5.Widgets import * 
    import cv2

imgCross = positioningCross(Dy, Dx, center, imgCross) 
cv2.imwrite("img.png", imgCross) 
self.ImgLabel.setPixmap(QPixmap("img.png")) 

DEF positioningCross(鏑,DX,中心,imgCross): 如果(中心[1,0]> =中心[ 0,0]): DY2 =中心[0,0] + np.absolute(DY) 否則: DY2 =中心[1,0] + np.absolute(DY)

if(center[0,1]>=center[1,1]): Dx2 = center[1,1] + np.absolute(Dx)/2 else: Dx2 = center[0,1] + np.absolute(Dx)/2 P1 = (center[0,1]/2,center[0,0]/2) P2 = (center[1,1]/2,center[1,0]/2) P3 = (Dx2/2,Dy2/2+100) P4 = (Dx2/2,Dy2/2-100) cv2.line(imgCross,(int(P1[0]),int(P1[1])),(int(P2[0]),int(P2[1])),(0,0,255),1) cv2.line(imgCross,(int(P3[0]),int(P3[1])),(int(P4[0]),int(P4[1])),(0,0,255),1) imgCross= cv2.flip(imgCross,1) return imgCross

我想畫兩個李將定位跨越到imgCross並將其顯示在我的GUI的標籤中。 目前,我將修改後的圖像保存在一個文件夾中,但我想知道是否可以將其添加到Label而不保存它?

我的解決方案是好的,但我想這可能會更好

有沒有人有想法?

回答

0

你的代碼有點不完整,但下面的內容應該會告訴你如何去做你想做的事情。

import sys 
from PyQt5 import QtGui, QtWidgets, QtCore 
from PyQt5.QtGui import QIcon, QPixmap 
from PyQt5.QtWidgets import * 

app = QApplication(sys.argv) 
label = QLabel() 
pixmap = QPixmap(32, 32) 
painter = QtGui.QPainter(pixmap) 
# Now draw whatever you like on the pixmap..no need to save to file 
painter.setPen(QtCore.Qt.red) 
painter.setBrush(QtGui.QBrush(QtCore.Qt.white)) 
rect = QtCore.QRect(0, 0, 31, 31) 
painter.drawRect(rect) 
painter.drawText(rect, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter, "foo") 
painter.end() 
label.setPixmap(pixmap) 

label.show() 

sys.exit(app.exec_()) 
+0

完美的作品,謝謝! – Tim

相關問題