I have a large image composed of subimages I have received from another program, I will eventually need to use the subimages as icons on buttons. I understand the math to access the bits + bytes of the QImage, it is described clearly in the below C++ question. What I don’t understand is how to obtain the offseted bits/memory location with PySide2.
Dividing QImage to smaller pieces
It’s a simple conversion of the above C++ to Python:
def myCreateSubImage(largeImage, subRect):
nOffset = subRect.x() * largeImage.depth() / 8 + subRect.y() * largeImage.bytesPerLine()
return QImage(largeImage.bits() + nOffset, subRect.width(), subRect.height(), largeImage.bytesPerLine(), subRect.format())
However:
-
Using “largeImage.bits() + nOffset” gives me a “No “”+”” function for <memory at 0x0000021EF8ABC700>” error.
-
Trying to treat it like a Python memoryview object like “(largeImage.bits())[nOffset]” gives me a “TypeError: a bytes-like object is required, not ‘NoneType'”
-
Coercing the image data to a QByteArray hasn’t worked, or I’m not doing it correctly. Trying to create it with a constructor (ie: QByteArray(largeImage.bits()) ) tells me there’s no constructor that takes a memoryview. Trying QByteArray.fromRawData(largeImage.bits()) tells me the same thing.