skip to Main Content

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


  1. 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:

     Duplicate the document - because we are going to be a bit destructive
     Select all & copy merged (Cmd-A,Shift+Cmd+C) 
     Flatten (Layer->Flatten Image)
     Fill with black (Edit->Fill->Black)
     Paste (the layer we copy merged)
     Lock transparent pixels on pasted layer (top of layers palette - first checkerboard icon on left)
     Fill with white (Edit->Fill->White)
     Add a color sampler at the pixel position you want
     Read sampler's red value (or green, or blue - they are all the same) and that is the transparency
    

    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:

    docLay[l].transparentPixelsLocked = true;
    

    and the samplers – where you might be well advised to clear all existing samplers (as only max 4 are permitted)

    var sampler = doc.colorSamplers.add([64, 64]);
    

    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.

    enter image description here

    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
  3. 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:

    enter image description here

    then load it into Photoshop and select the Color Sampler tol and move it around, you will see in the Info 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 as normal and opacity and fill at 100%. It will look like this:

    enter image description here

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search