Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing an image with Graphics View widget

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

but It gives this error:

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'

what should I do? thank you.

like image 534
hamid kavianathar Avatar asked Nov 27 '15 08:11

hamid kavianathar


1 Answers

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)) 
like image 62
Iron Fist Avatar answered Sep 30 '22 07:09

Iron Fist