Retrieving and using image data
When you want to start working with individual pixels, you can access this from individual layers or the document itself. The data format comes in a raw format called QByteArray. This format is a bit tricky to work with so let's go over what this is, and how to work with it for your images. Let's start by creating a simple 1x1 image and see what the data looks like.
We will get this as an output from our print statement.
There is some characters wrapped in a b'....' string. The stuff inside there is the data we have to work with. Since we are only working with one pixel, the data will be easier to pick apart. Each \xff represents a number. In our case all that data translates to the white color. With the four parts, we can assume each number corresponds to red, green, blue, or the alpha channel.
To verify that the RGBA is what is being stored, let's do another test changing one of the colors. In the previous example the canvas color was white. Now, let's set our foreground color to red, fill in the canvas, then check to see if we see the changes being updated.
And the output of the data looks like this in our Output
The data looks right on the output. The values that are set to 1.0 are set to \xff. The values that are 0.0 are set to \x00. We would expect to the color to be red. If we look at the canvas though and the filled in color, we see blue.
What is going on? If you play around with setting the values, you will notice that the red channel and the blue channel are flipped for some reason? Is this a bug? The reasoning is more of a historical reason with how image and color data were stored. BGR was once the standard format for working with images so many applications and standards had to support it. Either way, that is what we have to work with. As long as we know what is going on we will have to deal with it.
So we kind of understand the data that is being saved and how two of the channels are being swapped. Byte arrays are difficult to work with, so let's convert this data to a better format. Converting it to a QImage will give us more power. We can then query specifiy pixels and grab color data.
After converting the byte data to a QImage, it is much easier to work with now. One thing you will notice if you play around with the colors is that the red and blue values are still swapped. We could work knowing that, but there is also a helper function. We could swap out the QImage line to look like this from our previous example.
Now the channels are swapped. The downside of doing this is we will have to remember to swap it back later if we need to update the pixels on the image.
Note
Once we bring the byte array data and convert it to a Qimage, we are just working on the image data in memory. Changes we make will not affect what is on the layer. We will need to actually set the pixel data if we want to apply any changes.
Head to the Krita artists where we have a dedicated area for plugin development and give any feedback you might have.