QPixmap 保持纵横比

发布于 2025-01-07 04:23:06 字数 1299 浏览 2 评论 0原文

我正在编写一个程序,允许我通过他们的 API 将照片上传到 TUMBLR,我已经可以正常上传了(谢谢你们)。

我在 GUI 的一侧放置了一个“queueBox”,它显示图像名称,并且它们存储在 QListWidget 中。我已经把它放在我的主类的构造函数中:

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

并且我有这个方法:

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

这成功地将图像绘制到 myLabel 上,它是一个 QLabel,但是,它的缩放比例非常大,我

self.myLabel.setScaledContents(True)

在我的 ui_mainWindow 类中,如果我将其打开设置为 False,它修复了缩放比例,但仅显示图像的一小部分,因为图像比 QLabel 大得多。我想要的是能够保持纵横比,这样它看起来就不会缩放和可怕。

我发现了这个:http://www.riverbankcomputing.co。 uk/static/Docs/PyQt4/html/qpixmap.html 它说明了如何使用它,但是我无法让它按照我的评论中上面的代码所示工作。有谁知道如何使用这个?如果是这样,你能给我提供一个例子吗?我已经尝试过搜索,但我得到的大多数结果都是 C++ 中的工作示例,而不是 python。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿童谣 2025-01-14 04:23:06

摆脱

self.myLabel.setScaledContents(True)

呼叫(或将其设置为 False)。它用像素图填充您的小部件,而不关心宽高比。

如果您需要调整 QPixmap 的大小,正如您所发现的,scaled 是必需的方法。但你调用它是错误的。让我们看一下定义:

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

该函数的返回类型是QPixmap,因此它返回原始像素图的缩放副本

然后,您需要一个宽度和一个高度,描述像素图的(最大)最终尺寸。

还有两个可选参数。 aspectRatioMode 处理长宽比。 文档详细介绍了不同的选项及其效果。 transformMode 定义缩放的方式(哪种算法)。它可能会改变图像的最终质量。你可能不需要这个。

因此,将它们放在一起,您应该拥有(Qt 命名空间位于 QtCore 内部):

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

或者,如果您有固定大小的 QLabel,您可以调用.size() 方法从中获取大小:

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

注意:您可能需要使用 os.path.join(directory, tempName) 对于 目录 + '\\' + tempName 部分。

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.

梦幻的心爱 2025-01-14 04:23:06

PyQt5 代码更改更新:

avaris 的上述答案需要 PyQt5 更新,因为它损坏了。

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

self 保留在代码中会导致以下回溯错误。

TypeError:参数与任何重载调用都不匹配:scaled(self,int,int,aspectRatioMode:Qt.AspectRatioMode = Qt.IgnoreAspectRatio,transformMode:Qt.TransformationMode = Qt.FastTransformation):参数1具有意外类型“MainUI”缩放(自身,QSize,aspectRatioMode:Qt.AspectRatioMode = Qt.IgnoreAspectRatio,transformMode:Qt.TransformationMode = Qt.FastTransformation):参数1具有意外类型“MainUI”

因此这应该是(没有“self”,“Qt”)如下所述:

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

或:

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

KeepAspectRatio = 2...但是按照上述代码中的 aspectRatioMode = 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 = 2... but used as provided by aspectRatioMode = 2 in above code. Enjoy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文