PyQt6: How to set allocation limit in QImageReader?

Question:

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.

Asked By: Chaz Hollywood

||

Answers:

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

Answered By: ekhumoro

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

Answered By: kkkuloo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.