I’m trying to create my first python script with Krita. I’d like to take some pixel data from one document, rescale it and move it to another document’s layer. For now I could get the QByteArray, rescale it using a QImage, and then convert back this QImage to a QByteArray. This last byte array works fine when I try to put it again in a QImage (to save it and see the result for debug), but it fails when doing myLayer.setPixelData(pixelBytes, 0, 0,width, height) . Do you have any idea why / or where I could find some sample code for this ?
Here is the relevant part of my code :
pixelBytes = currentDocument.pixelData(0, 0, currentDocument.width(), currentDocument.height())
# Resize it through QImage
imageData = QImage(pixelBytes, currentDocument.width(), currentDocument.height(), QImage.Format_RGBA8888).rgbSwapped()
imageData.save(generationFolder + "img0.png") # Image is fine here
imageData = imageData.scaled(QSize(tilesetDoc.width(), tilesetDoc.height()), Qt.IgnoreAspectRatio)
imageData.save(generationFolder + "img1.png") # Image is fine here
# Converts QPixmap to bytes
bytes = QtCore.QByteArray()
buffer = QtCore.QBuffer(bytes)
buffer.open(QtCore.QIODevice.WriteOnly)
assert imageData.save(buffer, "PNG")
pixelBytes = QByteArray(bytes.data())
print(type(pixelBytes))
# Check if image is fine
img1 = QImage()
assert img1.loadFromData(pixelBytes)
img1.save(generationFolder + "img2.png") # Image is fine here
# Creates a layer for your image copy
root = tilesetDoc.rootNode()
tilesetLayer = tilesetDoc.createNode("tilesetImage", "paintlayer")
root.addChildNode(tilesetLayer, root)
# Sets image in layer
# Checks expected size of the image as said in the libkis docs (4 channels)
if imageData.sizeInBytes() == 4 * tilesetLayer.channels()[0].channelSize() * tilesetDoc.width() * tilesetDoc.height() :
print("Image size is correct")
# tilesetLayer.setPixelData(pixelBytes, 0, 0, tilesetDoc.width() , tilesetDoc.height()) # Krita crashes
I’ve also read about this other way to get the byte array, but doens’t work either for me :