Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
; 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.length); c++) {
lineArray
= new PathPointInfo; if (!theArray
[3]) {lineArray .kind = PointKind.CORNERPOINT} else {lineArray
.kind = theArray [3]}; lineArray
.anchor = theArray [0]; if (!theArray
[1]) {lineArray .leftDirection = theArray [0]} else {lineArray
.leftDirection = theArray [1]}; if (!theArray
[2]) {lineArray .rightDirection = theArray [0]} else {lineArray
.rightDirection = theArray [2]}; };
lineSubPathArray = new SubPathInfo();
lineSubPathArray.operation = ShapeOperation.SHAPEADD;
if (theArray[theArray.length - 1].constructor == Array) {lineSubPathArray.closed = true}
else {lineSubPathArray.closed = theArray[theArray.length - 1]};
lineSubPathArray.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 = [];
for (var c = 0; c < thePath.subPathItems.pathPoints.length; c++) {
var pointsNumber = thePath.subPathItems.pathPoints.length;
var theAnchor = thePath.subPathItems.pathPoints
.anchor; var theLeft = thePath.subPathItems.pathPoints
.leftDirection; var theRight = thePath.subPathItems.pathPoints
.rightDirection; var theKind = thePath.subPathItems.pathPoints
.kind; theArray
= [theAnchor, theLeft, theRight, theKind]; };
var theClose = thePath.subPathItems.closed;
theArray = theArray.concat(String(theClose))
};
app.preferences.rulerUnits = originalRulerUnits;
return theArray
};
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
And if you want the numbering to start at 1 instead of 0 replace
m
with
(m+1)
Copy link to clipboard
Copied
Awesome...!
Many, many thanks!
Jeff
Copy link to clipboard
Copied
c.pfaffenbichlerā i'm bringing this thread from the dead
I have this script that shows the percent of layer from entire canvas:
#target photoshop
var doc = activeDocument
var curLay = doc.activeLayer
getSelc ();
var his = doc.histogram
var hisCount = 0
for (var i=0;i<his.length;i++){
hisCount+= his
}
var perc= hisCount/(doc.width.value*doc.height.value)
doc.selection.deselect()
alert (perc);
function getSelc(){
var idsetd = charIDToTypeID( "setd" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref5 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref5.putProperty( idChnl, idfsel );
desc7.putReference( idnull, ref5 );
var idT = charIDToTypeID( "T " );
var ref6 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idTrsp = charIDToTypeID( "Trsp" );
ref6.putEnumerated( idChnl, idChnl, idTrsp );
desc7.putReference( idT, ref6 );
executeAction( idsetd, desc7, DialogModes.NO );
}
I need some help to combine it with your script, and only isolate layers that their percent from the entire canvas is larger than 15% for example, maybe even better: don't create layers that their subpath points number is lower than a certain number. (i have this picture and i need the object that is selected, and not always it's the biggest one or have the most points)
Copy link to clipboard
Copied
Hello,
i am trying to use the script bu this error comes up?
Any solutions to this? š
many thanks
Copy link to clipboard
Copied