[英]showing an image with Graphics View widget
我是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'
我該怎么辦? 謝謝。
您可以使用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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.