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

Display an image in a label without saving it

我的脚本中有以下几行:

    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

我想在 imgCross 中绘制两条带有 positioningCross 的线,并将其显示在我的 GUI 的标签中。 目前,我正在将修改后的图像保存在文件夹中,但我想知道是否可以在不保存的情况下将其添加到标签中?

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

有人有想法吗?

你的代码有点不完整,但下面应该会告诉你如何做你想做的事。

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