2017-10-11 149 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

相关问题