PyQt6:如何在 QImageReader 中设置分配限制?

发布于 2025-01-13 16:48:38 字数 851 浏览 3 评论 0原文

我正在将应用程序从 PyQt5 更新到 PyQt6。该应用程序使用非常大的图像文件。我已经更新了代码以使用 PyQt6,但是,当我运行 Python 脚本时,我现在收到错误:

QImageIOHandler:拒绝图像,因为它超出了当前分配 限制为 128 兆字节

请查看此处的 Qt6 文档: QImageReader::setAllocationLimit( )

...这里:QImageReader::allocationLimit()

文档建议 setAllocationLimit 可用于更改此 128 MB 限制。

我的问题是这些属性似乎没有出现在 Python 版本(PyQt6)中。这是 PyQt6 和 QImageReader 类的文档,并且 setAllocationLimit 和 AllocationLimit 不存在: QImageReader

我有什么遗漏的吗?我觉得如果我可以在 PyQt6 中访问 setAllocationLimit 它会解决我的问题,但我在任何地方都找不到它。

I'm updating an application from PyQt5 to PyQt6. The application uses very large image files. I've updated the code to work with PyQt6, however, when I run the Python script I now get an error:

QImageIOHandler: Rejecting image as it exceeds the current allocation
limit of 128 megabytes

Take a look at the Qt6 documentation here: QImageReader::setAllocationLimit()

...and here: QImageReader::allocationLimit()

The documentation suggests that setAllocationLimit can be used to change this 128 megabyte limit.

My issue is these attributes do not seem to appear in the Python version (PyQt6). Here's the documentation for PyQt6 and the QImageReader class and setAllocationLimit and AllocationLimit are not present: QImageReader.

Is there something I'm missing? I feel like if I can access setAllocationLimit in PyQt6 it would solve my problem, but I can't find it anywhere.

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

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

发布评论

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

评论(2

喜爱皱眉﹌ 2025-01-20 16:48:38

如果您使用 PySide6,您可以像这样禁用限制:

QtGui.QImageReader.setAllocationLimit(0)

并且不需要其他任何东西。

然而,对于 PyQt-6.3.1 及更早版本,此 API 目前尚未包装,这显然是一个错误。同时,解决方法是设置环境变量QT_IMAGEIO_MAXALLOC

>>> path = 'path/to/large-image.jpg'
>>> os.path.getsize(path) // 1024 // 1024
9
>>> r = QtGui.QImageReader(path)
>>> os.environ['QT_IMAGEIO_MAXALLOC'] = "1"
>>> r.read()
qt.gui.imageio: QImageIOHandler: Rejecting image as it exceeds the current allocation limit of 1 megabytes
>>> os.environ['QT_IMAGEIO_MAXALLOC'] = "10"
>>> r.read()
<PyQt6.QtGui.QImage object at 0x7f1d51857d10>

如果您希望在下一个 PyQt6 版本中看到上述错误得到修复,请在 邮件列表。维护者通常非常积极主动,因此应该很快修复它(假设它是一个相对简单的添加)。

更新

由于OP没有按照上述建议采取行动,我发布了添加 API 的请求

PS:PyQt-6.4.1中添加了allocationLimit()setAllocationLimit()

If you're using PySide6, you can disable the limit like this:

QtGui.QImageReader.setAllocationLimit(0)

and nothing else is needed.

However, for PyQt-6.3.1 and earlier, this API isn't currently wrapped, which is obviously a bug. In the meantime, a work-around is to set the environment variable QT_IMAGEIO_MAXALLOC:

>>> path = 'path/to/large-image.jpg'
>>> os.path.getsize(path) // 1024 // 1024
9
>>> r = QtGui.QImageReader(path)
>>> os.environ['QT_IMAGEIO_MAXALLOC'] = "1"
>>> r.read()
qt.gui.imageio: QImageIOHandler: Rejecting image as it exceeds the current allocation limit of 1 megabytes
>>> os.environ['QT_IMAGEIO_MAXALLOC'] = "10"
>>> r.read()
<PyQt6.QtGui.QImage object at 0x7f1d51857d10>

If you want to see the above bug fixed in the next PyQt6 release, please report it on the mailing list. The maintainer is usually very pro-active, so it should be fixed quite quickly (assuming it's a relatively straightforward addition).

UPDATE:

Since the OP didn't act on the above advice, I posted a request to have the API added.

PS: allocationLimit() and setAllocationLimit() were added in PyQt-6.4.1.

澉约 2025-01-20 16:48:38

对于 pyside6,建议的解决方案不起作用。
在 pyside6 文档中,我发现“QtGui.QImageReader.setAllocationLimit(0)”解决了我

写的问题:

from PySide6 import QtGui
os.environ['QT_IMAGEIO_MAXALLOC'] = "10000000000000000000000000000000000000000000000000000000000000000"
QtGui.QImageReader.setAllocationLimit(0)

之后,错误消失了,我能够将大图像插入到 qlabel 中

for pyside6, the proposed solution didn't work.
in the pyside6 documentation I found "QtGui.QImageReader.setAllocationLimit(0)" which solved my problem

i wrote:

from PySide6 import QtGui
os.environ['QT_IMAGEIO_MAXALLOC'] = "10000000000000000000000000000000000000000000000000000000000000000"
QtGui.QImageReader.setAllocationLimit(0)

after that, the error went away and I was able to insert a large image into the qlabel

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