4 Replies Latest reply on Feb 8, 2012 3:46 PM by uridicystar

    Detecting transperent pixels in a layer

    uridicystar Level 1


          I'm working on a script that saves out layers into multiple files.  However, I need to be able to tell if my current layer has any transperent pixels so I can decide what file type to save it as.  Is there a way to do this in extendscript?  I could probably get away with testing the upper left hand corner for transperency, but I'm unsure how to do this.


      Any ideas?


        • 1. Re: Detecting transperent pixels in a layer
          Michael L Hale Level 5

          Use scriptlistener to get the code to load the layer's transperency as a selection. Use the DOM to switch to quick mask mode and check that channel for any values other than 255.


          Something like this...


          function hasTransparency(){// assumes artlayer is activeLayer and no selection
              var res;
              var desc = new ActionDescriptor();
              var ref = new ActionReference();
              ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
              desc.putReference( charIDToTypeID( "null" ), ref );
              var ref1 = new ActionReference();
              ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );
              desc.putReference( charIDToTypeID( "T   " ), ref1 );
              executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
              app.activeDocument.quickMaskMode = true;
              var tempChannel = app.activeDocument.activeChannels[0].duplicate();
              app.activeDocument.quickMaskMode = false;
              tempChannel.histogram.toString().match(/[1-9][0-9]*,/g) != null ? res=true:res=false;
              return res;
          • 2. Re: Detecting transperent pixels in a layer
            uridicystar Level 1

            This was very helpful.  I think instead of checking all the possible values from the histogram, I'll just check the number of pixels with the value of 255 and see if it is equal to the total number of pixels in the document. 


            I didn't even know you could select the transperency that way.  Off to try it.  Thanks!

            • 3. Re: Detecting transperent pixels in a layer
              JJMack Most Valuable Participant

              Since CS3 Photoshop has shiped with a script names export layers to files and the are updated version on the web that supports exporting to PNG file.  When you wrote "working on a script that saves out layers into multiple files. " do you mean your updating or adding layers to existing image files?  If so do you want to write a layed file of a flat file?  Why not always  use save as PSD or PNG.

              • 4. Re: Detecting transperent pixels in a layer
                uridicystar Level 1

                Hey JJMack,

                    I'm saving out psd, rgb, and rgba files for my work. Unfortunately, I don't have a choice on the format. I end up with up to 3 different files per each psd I'm processing. Some of these files have an unknown number of layers(depending on the original file).  I didn't know there was a script that saved out layers, but I doubt it would have done everything I needed.  I have to use a plugin just to get CS5 to save SGI format files (.rgb, .rgba).


                On a side note:

                I did get this process working pretty well. However, the way I figure out if there is transperency sometimes causes Photoshop to display a warning dialog about.  The warning states that I'm selecting less than 50% of the pixels and the selection edge will be invisible.  I noticed someone else posted about this error before.  Has anyone figured out a good way to suppress this warning?  There's no checkbox to suppress it.