2

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Can you not scale down the images? That size is sort of excessive. As a user that sounds like huge download files, long loading times and memory usage on par with a dozen Chrome tabs. – Clashsoft Mar 13 '22 at 17:07
  • @Clashsoft I can't, it's for use in a piece of equipment that uses a very high resolution custom display. Memory usage and loading times aren't really an issue for the purpose of the equipment. The issue is suddenly there's a Qt6 memory limit now where there was none before. However, if I could change the image from 32bit to 16bit or 8 bit that should work. Not sure how to do that though. – Chaz Hollywood Mar 13 '22 at 17:24
  • After some experimenting, I tried converting the image down to 8 bit to see if that helped. Unfortunately, it's not possible to convert the image down to 8 bit because it's too large to be imported in the first place. I could slice the image up into smaller portions but I really don't want to. – Chaz Hollywood Mar 13 '22 at 18:37

2 Answers2

5

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you so much, this workaround totally worked! For those who just want the simple code: os.environ['QT_IMAGEIO_MAXALLOC'] = "insert MB limit you want here" That's all you need. Again, I really appreciate your help and I'll consider reporting it on that mailing list. – Chaz Hollywood Mar 13 '22 at 19:26
1

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

kkkuloo
  • 11
  • 2
  • The accepted answer already resolved the issue. Your answer did not add any new information. – The Pjot Jun 03 '22 at 13:15
  • without "QtGui.QImageReader.setAllocationLimit(0)", the proposed solution did not work on Pyside6 – kkkuloo Jun 03 '22 at 13:31
  • Ahh okay, can you edit your answer to clarify that? – The Pjot Jun 03 '22 at 13:36
  • As stated in my answer: "If you're using PySide6, you can disable the limit like this: `QtGui.QImageReader.setAllocationLimit(0)` - and nothing else is needed". – ekhumoro Feb 06 '23 at 14:10