显示带有图形视图小部件的图像 [英] showing an image with Graphics View widget
本文介绍了显示带有图形视图小部件的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 qt 设计器和 python 的新手.我想创建一个应该显示图像的简单项目.
我使用了Graphics View"小部件,并将其命名为graphicsView".我写了这些函数:
I'm new to qt designer and python. I want to created a simple project that I should display an image.
I used "Graphics View" widget and I named it "graphicsView". I wrote these function:
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'
我该怎么办?谢谢.
推荐答案
您也可以使用 QtGui.QLabel
来显示图像,方式如下:
You can use a QtGui.QLabel
to display images as well, this way:
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))
在你的原始代码中实现上面的代码:
Impplementing the above code in your original code:
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))
这篇关于显示带有图形视图小部件的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文