How to scale a image by QImageReader
-
I try to use the following code to scale a large image to a smaller size, but it failed. And thus because the too large size, the QImage is
NULL
.from PySide6.QtCore import QSize, QRect from PySide6.QtGui import QImageReader img = QImageReader('Image-0015.bmp') size = img.size() print(img.size()) img.setScaledSize(QSize(1024, 1024)) print(img.canRead()) print(img.size()) new_img = img.read() print(new_img)
-
@feiyuhuahuo said in How to scale a image by QImageReader:
but it failed. And thus because the too large size, the QImage is NULL.
I am not sure that is really the case?
img.setScaledSize(QSize(1024, 1024)) print(img.size())
Try
print(img.scaledSize())
?new_img = img.read() print(new_img)
If this returns a
null
image, tryprint(img.error(), img.errorString())
.I do not know what this is supposed to do
For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
-
@JonB I want to read a large BMP image (8200 * 6000, 47MB). I tried
QPixmap
andQImage
, but all of them failed.
Like:img = QImage('Image-0015.bmp') print(img)
I searched online and seems it's because that the image is to large. So I triedQImageReader
, did I do this right?
I don't know how to convert it to aQImage
object and how to scale it to a preferred size. -
@feiyuhuahuo
I now understand what you are showing. For a.bmp
which is 8200x6000 you can open it viaQImageReader
but not viaQImage
. This is not my area, butI searched online and seems it's because that the image is to large.
Where did you read this? In principle I think
QImage
is limited to 32767x32767, maybe larger, so your size would be "surprising" to be too large. Let's start with: What platform are you on? Are you compiling for 32- or 64-bit? What is the image's file size (ah, 47MB, sounds tiny!!)? How much free memory do you have? What version of Qt are you using?I also told you with your earlier code
If this returns a
null
image, tryprint(img.error(), img.errorString())
.Can you please actually do this, why guess what the issue might be when we can ask Qt?
-
@JonB I'm on a 64-bit WIN10. I have more than 20GB free memory. Qt version is 6.3.2. (PySide6 6.3.2, actually).
Here I did a test:from PySide6.QtGui import QImageReader, QImage from PIL import Image img = QImageReader('1000000139#OK#20220928101924#84.8.bmp') new_i = img.read() print(new_i, '\n') print(img.error(), '\n') print(img.errorString(), '\n') print('---------------------------------------') img = QImageReader('Image-0015.bmp') new_i = img.read() print(new_i, '\n') print(img.error(), '\n') print(img.errorString(), '\n') img1 = Image.open('1000000139#OK#20220928101924#84.8.bmp') print(img1.format) img2 = Image.open('Image-0015.bmp') print(img2.format)
As you can see, a 1024×1024 BMP image can be read. But the 8200×6000 BMP image can not be read. I used a image libraryPIL
to make sure they are actually BMP images. And they certainly are.
Besides,I read on some Chinese blogs. Someone else also encountered the same problem. They said it's because the image is too large. But I'm not sure about this. -
@feiyuhuahuo Have you tried increasing QImageReader::setAllocationLimit? The default limit seems to be 128MBytes (for an 8K image at 32bit according to the source).
-