用Graphics View小部件显示图像

7

我是新手,想尝试使用qt designer和python创建一个简单的项目,在这个项目中需要显示一张图片。
我使用了“图形视图”小部件并将其命名为“graphicsView”。我编写了以下函数:

def function(self):
    image=cv2.imread('C:/Users/Hamid/Desktop/1.jpg',1)
    self.show_frame_in_display(image)

def show_frame_in_display(self,frame):
    image = QImage(frame, frame.shape[1], frame.shape[0],
                  frame.strides[0], QImage.Format_RGB888)
    self.graphicsView.setPixmap(QPixmap.fromImage(image))

但它会出现以下错误:
File "C:/Users/Hamid/Documents/untitled/hamid.py", line 54, in show_frame_in_display
self.graphicsView.setPixmap(QPixmap.fromImage(image))
AttributeError: 'QGraphicsView' object has no attribute 'setPixmap'

我应该做什么?谢谢。


你可以使用“label”来显示图像,而不是... - Iron Fist
谢谢您的回复。您可以告诉我该如何使用吗? - hamid kavianathar
我刚刚发布了我的答案... - Iron Fist
1个回答

5
您可以使用QtGui.QLabel来显示图片,方法如下:
label_Image = QtGui.QLabel(frame)
image_path = 'c:\image_path.jpg' #path to your image file
image_profile = QtGui.QImage(image_path) #QImage object
image_profile = image_profile.scaled(250,250, aspectRatioMode=QtCore.Qt.KeepAspectRatio, transformMode=QtCore.Qt.SmoothTransformation) # To scale image for example and keep its Aspect Ration    
label_Image.setPixmap(QtGui.QPixmap.fromImage(image_profile)) 

将上述代码应用到您的原始代码中:

def function(self):
    image_path='c:\image_path.jpg' #path to your image file
    self.show_frame_in_display(image_path)

def show_frame_in_display(self,image_path):
    frame = QtGui.QWidget() #Replace it with any frame you will putting this label_image on it
    label_Image = QtGui.QLabel(frame)
    image_profile = QtGui.QImage(image_path) #QImage object
    image_profile = image_profile.scaled(250,250, aspectRatioMode=QtCore.Qt.KeepAspectRatio, transformMode=QtCore.Qt.SmoothTransformation) # To scale image for example and keep its Aspect Ration    
    label_Image.setPixmap(QtGui.QPixmap.fromImage(image_profile)) 

对不起,我使用了你的代码,但是出现了这个错误:'MyApp'对象没有属性'image_profile'。 - hamid kavianathar
在qt设计师中,我将标签命名为"label_image"。我应该在哪里使用它? - hamid kavianathar
好的...你把label_image放在哪个Widgetframe上了? - Iron Fist
对不起,我有点困惑。在Qt Designer中,我拖动了一个标签,并将其对象名称更改为“lable_image”,然后保存它,现在我要从内存中读取图像。您能给我提供更多指导吗? - hamid kavianathar
1
我使用了一个标签。非常简单。感谢 Khalil Ammour 的帮助和关注。 - hamid kavianathar
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接