showing an image with Graphics View widget

2024/10/7 0:15:58

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.

Answer

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 fileself.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 itlabel_Image = QtGui.QLabel(frame)image_profile = QtGui.QImage(image_path) #QImage objectimage_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)) 
https://en.xdnf.cn/q/70309.html

Related Q&A

TemplateSyntaxError: settings_tags is not a valid tag library

i got this error when i try to run this test case: WHICH IS written in tests.py of my django application:def test_accounts_register( self ):self.url = http://royalflag.com.pk/accounts/register/self.c =…

Setting NLTK with Stanford NLP (both StanfordNERTagger and StanfordPOSTagger) for Spanish

The NLTK documentation is rather poor in this integration. The steps I followed were:Download http://nlp.stanford.edu/software/stanford-postagger-full-2015-04-20.zip to /home/me/stanford Download http:…

python variable scope in nested functions

I am reading this article about decorator.At Step 8 , there is a function defined as:def outer():x = 1def inner():print x # 1return innerand if we run it by:>>> foo = outer() >>> foo.…

How can I throttle Python threads?

I have a thread doing a lot of CPU-intensive processing, which seems to be blocking out other threads. How do I limit it?This is for web2py specifically, but a general solution would be fine.

get lastweek dates using python?

I am trying to get the date of the last week with python. if date is : 10 OCT 2014 meansIt should be print10 OCT 2014, 09 OCT 2014, 08 OCT 2014, 07 OCT 2014, 06 OCT 2014, 05 OCT 2014, 04 OCT 2014I trie…

Why is vectorized numpy code slower than for loops?

I have two numpy arrays, X and Y, with shapes (n,d) and (m,d), respectively. Assume that we want to compute the Euclidean distances between each row of X and each row of Y and store the result in array…

Handle TCP Provider: Error code 0x68 (104)

Im using this code to sync my db with the clients:import pyodbcSYNC_FETCH_ARRAY_SIZE=25000# define connection + cursorconnection = pyodbc.connect()cursor = connection.cursor()query = select some_column…

vectorized radix sort with numpy - can it beat np.sort?

Numpy doesnt yet have a radix sort, so I wondered whether it was possible to write one using pre-existing numpy functions. So far I have the following, which does work, but is about 10 times slower tha…

Which library should I use to write an XLS from Linux / Python?

Id love a good native Python library to write XLS, but it doesnt seem to exist. Happily, Jython does.So Im trying to decide between jexcelapi and Apache HSSF: http://www.andykhan.com/jexcelapi/tutoria…

put_records() only accepts keyword arguments in Kinesis boto3 Python API

from __future__ import print_function # Python 2/3 compatibility import boto3 import json import decimal#kinesis = boto3.resource(kinesis, region_name=eu-west-1) client = boto3.client(kinesis) with ope…