5 Replies Latest reply on Mar 17, 2012 5:08 AM by Jeff S GP2

    Isolating groups of pixels

    Jeff S GP2 Level 1

      I have a transparent  layer with 40 -50 blocks of color in random areas.

       

      Is there a way of isolating each block of color and placing them on their own layer?

       

      Thanks!

       

      Jeff

        • 1. Re: Isolating groups of pixels
          c.pfaffenbichler Level 9

          Could you give this a try?

          // lift discontinuous content from layer onto individual layers;
          // for rgb-images and cs5;
          // 2012, use it at your own risk;
          #target photoshop
          if (app.documents.length > 0 && app.activeDocument.mode = DocumentMode.RGB) {
          myDocument = app.activeDocument;
          myDocument.activeChannels = [myDocument.channels[0], myDocument.channels[1], myDocument.channels[2]];
          // load transarency;
          // =======================================================
          var idsetd = charIDToTypeID( "setd" );
              var desc11 = new ActionDescriptor();
              var idnull = charIDToTypeID( "null" );
                  var ref6 = new ActionReference();
                  var idChnl = charIDToTypeID( "Chnl" );
                  var idfsel = charIDToTypeID( "fsel" );
                  ref6.putProperty( idChnl, idfsel );
              desc11.putReference( idnull, ref6 );
              var idT = charIDToTypeID( "T   " );
                  var ref7 = new ActionReference();
                  var idChnl = charIDToTypeID( "Chnl" );
                  var idChnl = charIDToTypeID( "Chnl" );
                  var idTrsp = charIDToTypeID( "Trsp" );
                  ref7.putEnumerated( idChnl, idChnl, idTrsp );
              desc11.putReference( idT, ref7 );
          executeAction( idsetd, desc11, DialogModes.NO );
          //
          var check = true;
          // set to pixels;
          var originalRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          // set to 72ppi;
          var originalResolution = app.activeDocument.resolution;
          myDocument.resizeImage (undefined, undefined, 72, ResampleMethod.NONE);
          //
          var theMask = myDocument.channels.add();
          myDocument.selection.store(theMask);
          myDocument.activeChannels = [myDocument.channels[0], myDocument.channels[1], myDocument.channels[2]];
          //var theMask = myDocument.channels[3];
          var theLayer = myDocument.activeLayer;
          myDocument.selection.load(theMask, SelectionType.REPLACE);
          // use threshold to heighten non black pixels;
          myDocument.quickMaskMode = true;
          myDocument.activeLayer.threshold(1);
          myDocument.quickMaskMode = false;
          // create work path;
          myDocument.selection.makeWorkPath(1);
          var subPathsNumber = myDocument.pathItems[myDocument.pathItems.length-1].subPathItems.length;
          // check for a high number of paths;
          if (subPathsNumber > 30) {
                    check = confirm("warning\rthere appear to be "+subPathsNumber+" elements.\rif this number is too high noise may have caused them.\rthis may result in a crash.\rproceed?")
                    };
          // do the operation;
          if (check == true) {
                    var thePathInfo = collectPathInfo(myDocument, myDocument.pathItems[myDocument.pathItems.length-1]);
          // create one path for every subpathitem:
                    for (var m = 0; m < thePathInfo.length - 1; m++) {
                              var theSubPath = thePathInfo[m];
                              var aPath = createPath ([theSubPath], m+"_path");
                              aPath.makeSelection(0, true, SelectionType.REPLACE);
          // expand;
                              myDocument.selection.expand(6);
                              myDocument.selection.load(theMask, SelectionType.INTERSECT);
          // layer via copy;
                              try {
                                        var id14 = charIDToTypeID( "CpTL" );
                                        executeAction( id14, undefined, DialogModes.NO );
                                        myDocumentctiveLayer.name = theLayer.name + "_" + m
                                        }
                              catch (e) {};
                              myDocument.pathItems[myDocument.pathItems.length-1].remove();
                              myDocument.activeLayer = theLayer;
                              };
                    theLayer.visible = false;
                    };
          // reset;
          app.preferences.rulerUnits = originalRulerUnits;
          myDocument.resizeImage (undefined, undefined, originalResolution, ResampleMethod.NONE);
          };
          ////////////////////////////////////
          ////////////////////////////////////
          ////////////////////////////////////
          ////// function to create path from array with one array per point that holds anchor, leftdirection, etc, 2010 //////
          function createPath (theArray, thePointsName) {
          var originalRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.POINTS;
          lineSubPathArray = new Array ();
          if (theArray[theArray.length - 1].constructor != Array) {var numberOfPoints = theArray.length - 1}
          else {var numberOfPoints = theArray.length};
          for (var b = 0; b < numberOfPoints; b++) {
                    var lineArray = new Array ();
                    for (c = 0; c < (theArray[b].length); c++) {
                              lineArray[c] = new PathPointInfo;
          
                              if (!theArray[b][c][3]) {lineArray[c].kind = PointKind.CORNERPOINT}
                              else {lineArray[c].kind = theArray[b][c][3]};
          
                              lineArray[c].anchor = theArray[b][c][0];
          
          
                              if (!theArray[b][c][1]) {lineArray[c].leftDirection = theArray[b][c][0]}
                              else {lineArray[c].leftDirection = theArray[b][c][1]};
          
                              if (!theArray[b][c][2]) {lineArray[c].rightDirection = theArray[b][c][0]}
                              else {lineArray[c].rightDirection = theArray[b][c][2]};
          
                              };
                    lineSubPathArray[b] = new SubPathInfo();
                    lineSubPathArray[b].operation = ShapeOperation.SHAPEADD;
          
                    if (theArray[b][theArray[b].length - 1].constructor == Array) {lineSubPathArray[b].closed = true}
                    else {lineSubPathArray[b].closed = theArray[b][theArray[b].length - 1]};
          
                    lineSubPathArray[b].entireSubPath = lineArray;
                    };
          var myPathItem = app.activeDocument.pathItems.add(thePointsName, lineSubPathArray);
          app.preferences.rulerUnits = originalRulerUnits;
          return myPathItem
          };
          ////// function to collect path-info as text //////
          function collectPathInfo (myDocument, thePath) {
          var originalRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.POINTS;
          var theArray = [];
          for (var b = 0; b < thePath.subPathItems.length; b++) {
                    theArray[b] = [];
                    for (var c = 0; c < thePath.subPathItems[b].pathPoints.length; c++) {
                              var pointsNumber = thePath.subPathItems[b].pathPoints.length;
                              var theAnchor = thePath.subPathItems[b].pathPoints[c].anchor;
                              var theLeft = thePath.subPathItems[b].pathPoints[c].leftDirection;
                              var theRight = thePath.subPathItems[b].pathPoints[c].rightDirection;
                              var theKind = thePath.subPathItems[b].pathPoints[c].kind;
                              theArray[b][c] = [theAnchor, theLeft, theRight, theKind];
                              };
                    var theClose = thePath.subPathItems[b].closed;
                    theArray = theArray.concat(String(theClose))
                    };
          app.preferences.rulerUnits = originalRulerUnits;
          return theArray
          };
          
          • 2. Re: Isolating groups of pixels
            Jeff S GP2 Level 1

            Wow,  this works great!!!

             

            Thanks,  c.pfaffenbichler

             

            It even isolates color areas down to a few pixels in size...  Perfect for what I'm looking for!

             

            ...

             

            ...Could I perhaps impose to ask one small additional feature?

             

            Would it be possible to have the new layers created be given a unique name...  something like: Area 1, Area 2  etc..  (or anything)

             

            This would make it ideal to isolate for further Actions being applied.

             

            If not, no worries..  This is perfect for what I'm looking for

             

            Many thanks!!

             

            Jeff

            • 3. Re: Isolating groups of pixels
              c.pfaffenbichler Level 9

              I think you would just need to change the line (there is a typo, myDocumentctiveLayer should be myDocument.activeLayer)

                                            myDocumentctiveLayer.name = theLayer.name + "_" + m 

              to

                                            myDocument.activeLayer.name = "Area " + m 


              • 4. Re: Isolating groups of pixels
                c.pfaffenbichler Level 9

                And if you want the numbering to start at 1 instead of 0 replace

                m

                with

                (m+1)

                • 5. Re: Isolating groups of pixels
                  Jeff S GP2 Level 1

                  Awesome...!

                   

                  Many, many  thanks!  

                   

                  Jeff