How can we obtain the pixels value in RGB or HSB of the image

Hello!
Im using:
app.activeDocument.colorSamplers.add({x: centerX, y: centerY});
px = app.activeDocument.colorSamplers[0];

in order to read the pixel of an image but the process is very slow so it’s not useful this way.
is there another way to obtain the pixel information in RGB or HSB in a better process.
so once that we have that info we can used as we wanted.

Pleas, need to know and thanks.

@HimagenesPrint I think the Imaging API is what will provide the pixel information you want.
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/media/imaging/

Thanks for responding but I didn’t see anything using the models, instances and examples but nothing could help me get the RGB pixel values. As I said, colorSample provides that information but you have to go through the image to obtain the values, which makes this a very slow process. Can anyone help?

Did you go through the doc, that @samgannaway linked? Imaging API literally provides all the pixels’ values in RGB. Did you try to get those?

1 Like

Thanks again.
When going through the objects in that info, I could see that they have information about the pixels as a group or quantity and I saw that they have pixels by luminosity but that pixel is as information of the document. My problem is I couldn’t see any array of pixels and that pixel with the RGB value or the HSB description according to the pixel, what I could not find is how to obtain a pixel and see the value of how much over the pixel is Red, how much is Green and how much is Blue pixel by pixel. what mean is: If an image has, for example, 1,000 pxs, you can obtain the RGB information from the pixel by pixel.
example:
pixel1[red]
pixel1[green]
pixel1[blue],

that’s what I couldn’t get in that link,
Using the other way app.activeDocument.colorSamplers[0] I can get :
color.rgb.red,
color.rgb.green
color.rgb.blue over a pixel that I point.
I can locate the pixel and get that pixel information divided into its values.
In the information contained in the link I do not see how to obtain the values ​​of the pixels by pixels in the same way.
Can please show me what I’m messing

Is the problem obtaining a pixel at a particular (x, y) coordinate vs. a linear index? You can easily compute that index using width and height and number of components (in this case 3). To clarify, what you get through the imaging API is pixels data as a flat array (accessed by one index). You won’t get an array of “pixel objects” (that would also be kind of insane to store :wink:). But you can get red, green, blue for a pixel by computing the right index and offsets into that array. What gets returned is a “chunky” array, so at least red, green, blue are stored one after the other.

Not sure, but maybe this will be useful. It reads the color of a specific point on the image with coordinates (0,0) and provides the red, green, and blue values.

const colorSamplerList = await batchPlay(
            [ { _obj:"colorSampler", _target:{_ref: "document",_enum: "ordinal",_value: "targetEnum"}, samplePoint: { horizontal: 0, vertical: 0 } }],{});
        
        const red=colorSamplerList[0].colorSampler.red;
        const green=colorSamplerList[0].colorSampler.grain;
        const blue=colorSamplerList[0].colorSampler.blue; 

I’ve been asking for this feature since the very first Beta introduction of imaging API. Then I asked during one of live sessions and there was a promise to look into it, but I assume that promise was never fulfilled and my requests were completely ignored :man_shrugging:

Having said that, as @dotproduct mentioned, you can still calculate the starting index of the chunky data array ([R1, G1, B1, R2, G2, B2, ...]) - idx = ((y * w) + x) * c, where x and y are your pixel coordinates (zero based - 1st being 0,0), w - image width, and c - channel count. You need channel count, because, IIRC, if image hasAlpha is true, the data array includes 4th channel and result would be [R1, G1, B1, A1, R2, G2, B2, A2, ...]

With above formula, let’s say you have 10x10 image and you want to find RGB of a pixel at 4,2 (25th; index 24). Your indexes would be 72 for R, 73 for G and 74 for B (without alpha channel)

Disclaimer: I did not test that, so some things might be wrong

Thanks once more!
I will try and see

I don’t think you’re being very charitable, here, Kamalakas. @amandah and @pklaschka definitely investigate requests from live sessions.

2 Likes