QPixmap maintain aspect ratio

2019-02-11 03:13发布

问题:

I'm writing a program that will allow me to upload photos to TUMBLR via their API, I've got the uploading working (thanks to you guys).

I've put a 'queueBox' on the side of the GUI, which displays the image names, and they are stored in a QListWidget. I've put this in my Main Class' constructor:

def __init__(self):
    QtGui.QMainWindow.__init__(self)
    self.setupUi(self)
    self.queueBox.itemClicked.connect(self.displayPhoto)

and I have this method:

def displayPhoto(self, item):
    tempName = (item.text())
    print tempName
    self.myLabel.setPixmap(QtGui.QPixmap(_fromUtf8(directory + '\\' + tempName)))  
    ## self.myLabel.pixmap(QPixmap.scaled(aspectRatioMode = Qt.IgnoreAspectRatio))
    ## ^ ^ ^ What do I do with this? How do I set it to maintain aspect ratio?
    ## Currently it says ''NameError: global name 'Qt' is not defined''

This sucessfully draws the image on to myLabel which is a QLabel, however, It is very scaled, I have

self.myLabel.setScaledContents(True)

in my ui_mainWindow class, and if I turn it to False, it fixes the scaling but it only shows a small portion of the image because the image is much larger than the QLabel. What I want is to be able to maintain the aspect ratio, so it doesn't look scaled and horrible.

I found this: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpixmap.html and it says how to use it, however I can't get it to work as shown in the code above in my comments. Does anyone know how to use this? If so, can you provide me with an example, I've tried searching but most of the results I get are working examples in C++, not python.

Thanks!

回答1:

Get rid of the

self.myLabel.setScaledContents(True)

call (or set it to False). It is filling your widget with the pixmap without caring about the aspect ratio.

If you need to resize a QPixmap, as you have found, scaled is the required method. But you are invoking it wrong. Let's look at the definition:

QPixmap QPixmap.scaled (self, 
                        int width, 
                        int height, 
                        Qt.AspectRatioMode aspectRatioMode = Qt.IgnoreAspectRatio,
                        Qt.TransformationMode transformMode = Qt.FastTransformation)

Return type of this function is QPixmap, so it returns a scaled copy of the original pixmap.

Then you need a width and a height, describing the (maximum) final size of the pixmap.

Two more optional parameters. aspectRatioMode deals with the, well aspect ratio. The documentation details the different options and their effects. transformMode defines how (which algorithm) the scaling is done. It might change the final quality of your image. You probably don't need this one.

So, putting it together you should have (Qt namespace is inside QtCore):

# substitute the width and height to desired values
self.myLabel.setPixmap(QtGui.QPixmap(_fromUtf8(directory + '\\' + tempName)).scaled(width, height, QtCore.Qt.KeepAspectRatio))

Alternatively, if you have a fixed size QLabel, you could call the .size() method to get the size from it:

self.myLabel.setPixmap(QtGui.QPixmap(_fromUtf8(directory + '\\' + tempName)).scaled(self.myLabel.size(), QtCore.Qt.KeepAspectRatio))

Note: You might want to use os.path.join(directory, tempName) for the directory + '\\' + tempName part.



回答2:

PyQt5 code change update:

The above answer of avaris needed a PyQt5 update because it breaks.

QPixmap.scaled (self, int width, int height, Qt.AspectRatioMode aspectRatioMode = Qt.IgnoreAspectRatio

Keeping the self in the code results in below traceback error.

TypeError: arguments did not match any overloaded call: scaled(self, int, int, aspectRatioMode: Qt.AspectRatioMode = Qt.IgnoreAspectRatio, transformMode: Qt.TransformationMode = Qt.FastTransformation): argument 1 has unexpected type 'MainUI' scaled(self, QSize, aspectRatioMode: Qt.AspectRatioMode = Qt.IgnoreAspectRatio, transformMode: Qt.TransformationMode = Qt.FastTransformation): argument 1 has unexpected type 'MainUI'

Thus this should be (without "self", "Qt") as stated below:

QPixmap.scaled (int width, int height, aspectRatioMode = IgnoreAspectRatio

or:

QPixmap.scaled (int width, int height, aspectRatioMode = 0)

KeepAspectRatio = 4... but used as provided by aspectRatioMode = 4 in above code. Enjoy!