在标签中显示图像而不保存

时间:2017-10-11 10:38:27

标签: python image pyqt5 qpixmap cv2

我的脚本中有以下几行:

    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(Dy, Dx, center, imgCross): if(center[1,0]>=center[0,0]): Dy2 = center[0,0] + np.absolute(Dy) else: Dy2 = center[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

我想使用positioningCross将两行绘制到imgCross中,并将其显示在GUI的标签中。 目前,我已将修改后的图像保存在文件夹中,但我想知道是否可以将其添加到标签中而不保存它?

我的解决方案还可以,但我想它可能会更好

有人有想法吗?

1 个答案:

答案 0 :(得分:2)

您的代码有点不完整,但以下内容应该告诉您如何做您想做的事。

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