Python 2.7 如何在Qt标签中显示图像?
我在Qt label中的窗体上显示图像时遇到了一些问题…“b”是我传递给构造函数的图像,当我在构造函数中显示图像时,图像会显示出来,但当我在label中设置图像时,我仍然得到一个没有图像的窗体。它也不会显示任何错误Python 2.7 如何在Qt标签中显示图像?,python-2.7,opencv,pyqt4,Python 2.7,Opencv,Pyqt4,我在Qt label中的窗体上显示图像时遇到了一些问题…“b”是我传递给构造函数的图像,当我在构造函数中显示图像时,图像会显示出来,但当我在label中设置图像时,我仍然得到一个没有图像的窗体。它也不会显示任何错误 def on_clicked_micro(self): self.obj3=MyForm2(b=self.blur) self.obj3.show() self.hide() class MyForm2(QtGui.QMainWind
def on_clicked_micro(self):
self.obj3=MyForm2(b=self.blur)
self.obj3.show()
self.hide()
class MyForm2(QtGui.QMainWindow,Ui_image3):
def __init__(self,parent=None,b=None):
QtGui.QMainWindow.__init__(self,parent)
self.imgPreProc=imagePreProcessor()
cv2.imshow('blur',b) #############displaying the image #############
self.label = QtGui.QLabel(self)
self.setupUi(self)
self.label.setPixmap(QtGui.QPixmap(b))
变量
b
的类型是从cv2.imread('picture.bmp')
返回的。类QtGui.QPixmap
是按文件名(或路径)传递图像,而不是cv对象。它不应该显示出来
它有可能以两种方式修复它们
1) 您可以使用设置的数组数据、宽度和高度在QtGui.QImage
中转换它们(例如转换,您可以阅读答案或博客)。并使用转换为QtGui.QPixmap
。并使用相同的方法将其加载到QtGui.QLabel
中。不要忘记设置QtGui.QLabel
的布局和大小。如果您传入的图像QtGui.QLabel
非常小。我看不见他们
class MyForm2(QtGui.QMainWindow,Ui_image3):
def __init__(self,parent=None,b=None):
QtGui.QMainWindow.__init__(self,parent)
self.imgPreProc=imagePreProcessor()
cv2.imshow('blur',b)
self.label = QtGui.QLabel(self)
self.setupUi(self)
myQImage = self.imageOpenCv2ToQImage(b)
self.label.setPixmap(QtGui.QPixmap.fromImage(myQImage))
def imageOpenCv2ToQImage (self, cv_img):
height, width, bytesPerComponent = cv_img.shape
bytesPerLine = bytesPerComponent * width;
cv2.cvtColor(cv_img, cv2.CV_BGR2RGB, cv_img)
return QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
2) 添加变量path image file name
,将图像直接传递给构造函数中的QtGui.QPixmap
。这比解决第一题容易。不要忘记设置QtGui.QLabel的布局和大小
class MyForm2(QtGui.QMainWindow,Ui_image3):
def __init__(self, pathFileName, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.imgPreProc = imagePreProcessor()
cv2.imshow('blur', cv2.imread(pathFileName))
self.label = QtGui.QLabel(self)
self.setupUi(self)
self.label.setPixmap(QtGui.QPixmap(pathFileName))