I want to write some PS Javascript code to get a pixel’s ARGB color value. A function which would look like:
function getPixelARGB(doc, x, y);
I searched for a while and found a method by Mike Hale
in this site: a link
The answer proposed by Mike Hale
works fine, the only problem is that it can’t get the alpha value(opacity) of the selected pixel, which is what I want to know.
Anybody have any idea on how to get the ARGB value of a selected pixel by PS script? Thanks in advance.
3
Answers
I can tell you briefly how to do it, but I don’t have time to write the code this very minute – maybe over the next couple of days if you are lucky. So, hopefully your Extendscript skills are reasonable…. 🙂
The issue is that the Color Sampler doesn’t pass back transparency, just RGB values. So, the technique will be to force the transparency into the RGB values so you can use them. Here are the steps:
Most of the Extendscript for the above is fairly simple, the only two harder bits are setting the transparent pixels to
locked
, which you do like this:and the samplers – where you might be well advised to clear all existing samplers (as only max 4 are permitted)
I have made a little animated GIF here that shows you the process. I start with a
red->transparent
gradient image. Note at the end when I have the color dropper tool running, you will see the greyscale values in the Color Info window change as I move up and down the image with the mouse, these reflect the original transparency.Ok, if you want to support Photoshop CS2, I have something that will work, but is quite
hacky
– to say the least.The basic idea is to get ImageMagick to do it on behalf of Photoshop – and that is probably quicker than anything accessing individual pixels in Photoshop’s ExtendScript anyway. So, the ImageMagick command to see pixels in text/human-readable form is this:
You can see the transparency is
FE
or 0.996078 for this row.So, if you want 1 pixel, say the one at 128,128, you would do this:
and it has an opacity of
7F
or 0.498039.So, to realise what you want to do, your putative function
getPixelARGB(doc, x, y)
will have to do the following steps:So, how do you invoke ImageMagick and read its output? You can use this:
I might as well go for a third method while I am thinking about this… 🙂
If you create a gradient image which is solid red to transparent, like this:
then load it into Photoshop and select the
Color Sampler
tol and move it around, you will see in theInfo
window, that Photoshop reports it as pure red wherever you move the sampler – basically ignoring the transparency.Now, create a new layer and fill it with black, and then in the
Layers
window, drag the new black layer below the transparent one – leave the blending mode asnormal
andopacity
andfill
at 100%. It will look like this:Now move the Color Sampler around and you will see the
Red
RGB value is directly inversely proportional to the opacity – and now the GOOD NEWS – you can get the red channel with Extendscript! So, the technique would be to get the red pixel’s RGB value, then put the layer behind and get the red pixel’s value again and the difference between the two is proportional to the opacity.I guess if you filled with white instead of black, the red channel would be directly proportional to the opacity.