Hi there, I am wondering if someone could help explain what I’m seeing with my usage of the getPixel command on an image. My image is a 4x1 rgba pixel image that looks like the following (i"ve enlarged it so you can see it):
I’ve put a red colour on the 1st and 3rd pixels, and left the 2nd and 4th pixel as transparent
I have the following code to print out the unsigned pixelValue for each pixel
local activeImage = app.activeImage
for x = 0, 3 do
And this is what it prints out:
My question is why are the two “transparent” pixels reading as different values?
getPixel() is a function that works on cels, not documents. As such, getPixel() works within the cel’s coordinates, which are not the same as canvas coordinates (the documentation illustrates this)! When accessing 3,0, you’re accessing a pixel that doesn’t exist within the cel (because transparent pixels on all sides are trimmed away), so you’re getting garbage data.
You need to subtract the cel’s position from your desired coordinates to get the coordinates within the cel, and you also need to check that you’re within the cel’s bounds, not the canvas’s bounds.
Ok thanks for the clarification, yes I noticed the transparent pixels getting trimmed and that threw me off. When you say getPixel() works on cels, is that synonymous with it working on “image” objects? The function looks to be part of the Image object class.
And a follow up question, how does one draw pixels to an area that is outside of an Image/cell bounds? I’ve been using Image:drawPixel() but nothing happens when I give it a coordinate that is outside of the image bounds (but still within the canvas).
A cel is a positioned Image, so they’re almost synonymous but not quite.
I haven’t played with drawPixel() so I don’t know, but I’d assume drawing out of bounds would change the bounding box the same way using drawing tools manually does. It’s something to try, I guess.
The bounding box changes may throw off your loops, so it’s probably best to loop in canvas coordinates and fetch and calculate the Image/cel pixel coordinates with every iteration if you’re using drawPixel(). To avoid this issue, you could create your own Image, draw to it, and then use drawImage() to put the data into the cel.