Get pixel ARGB color by Photoshop Javascript


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.


Answers:


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:

convert out.png txt:

# ImageMagick pixel enumeration: 256,256,255,srgba
0,1: (255,0,0,0.996078)  #FF0000FE  srgba(255,0,0,0.996078)
1,1: (255,0,0,0.996078)  #FF0000FE  srgba(255,0,0,0.996078)
2,1: (255,0,0,0.996078)  #FF0000FE  srgba(255,0,0,0.996078)
3,1: (255,0,0,0.996078)  #FF0000FE  srgba(255,0,0,0.996078)

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:

convert out.png -crop 1x1+128+128 -depth 8 txt:
# ImageMagick pixel enumeration: 1,1,255,srgba
0,0: (255,0,0,0.498039)  #FF00007F  srgba(255,0,0,0.498039)

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:

1. duplicate document `doc`
2. save duplicate as `PNG` (to preserve transparency) on somewhere like `/tmp`
3. invoke ImageMagick - see below
4. read result - see below

So, how do you invoke ImageMagick and read its output? You can use this:

app.system("convert /tmp/tmp.png -crop 1x1+128+128 -depth 8 txt: > /tmp/result.txt")
var w = new File("/tmp/result.txt");
w.open('r');
var str = "";
while(!w.eof)
   str += w.readln();
w.close();   
alert(str);