Skip to main content
Participating Frequently
March 11, 2014
Question

Auto cut-up / layer of photo by geometric mask

  • March 11, 2014
  • 2 replies
  • 3175 views

Hi I know nothing about scripting and I'm looking for some help please.

Is it possible for a script to use a geomtric grid, pasted as a path from AI as a vector mask, to automatically cut up a photo and create a new layer for each section of the grid?

I'm trying to cut up pictures which look like this one below automatically, so that I can concentrate on the process of moving, rotating and rearranging the tiles:

I'm fairly new to PS and very new to AI.  These are just rough tests as I learn the processes.

Here's a link to my open question in the General Photoshop forum so you can get more of background on what I want to do:

http://forums.adobe.com/message/6199129#6199129

Any help from someone who knows script well would be great.

Thanks

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
March 12, 2014

If you open attched jpg and see the Paths Panel you can see that each of the subPathItems is a closed triangle.

Is your Path like that?

But this still leaves the issue that if the segments do not line up with the pixel grid some triangles of ostensibly equal dimensions may be somewhat off due to rounding effects. (edited)

Participating Frequently
March 12, 2014

Thanks, I see what you mean. This works perfectly to select different regions and great new layers by the Paths.  I can't seem to get this to work on my version (I posted psd and ai files here: https://dl.dropboxusercontent.com/u/14000142/GRID%20TO%20PATH.psd and https://dl.dropboxusercontent.com/u/14000142/GRID%20TEST.ai)

So, I think it must be my Paths that I haven't got the hang of.  In my psd, I can't just select a selection, I have to actually click on the Path lines to select anything... and even then, it won't select pixels from main photo layer.

c.pfaffenbichler, could you show me where I'm going wrong when I'm creating my path please?

Once this is sorted and I've learnt how to create the Paths properly, would someone be interested in writing a simple script to automatically cut/create new layers by path to help me?

Thanks, Henry

c.pfaffenbichler
Community Expert
Community Expert
March 12, 2014

This works only for angular, non round paths and the rounding issues apply, but maybe it can help somewhat.

// split according to selected path’s subpathitem’s pathpoints;

// bezier handles are being ignored.;

// 2014, use at your own risk;

#target photoshop

if (app.documents.length > 0) {

try {

var time1 = Number(timeString());

app.activeDocument.suspendHistory("stuff", "main ()");

var time2 = Number(timeString());

alert(((time2-time1)/1000)+" seconds\nstart "+time1+"\nend "+time2);

}

catch (e) {}

};

////// function //////

function main () {

// set to pixels;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS

var myDocument = app.activeDocument;

var theLayer = myDocument.activeLayer;

// set to 72ppi;

var originalResolution = app.activeDocument.resolution;

myDocument.resizeImage (undefined, undefined, 72, ResampleMethod.NONE);

// get the path;

var thePath = collectPathInfoFromDesc2012 (myDocument, selectedPath ());

// create the layers;

for (var m = 0; m < thePath.length; m++) {

var theSubPath = thePath;

var theArray = new Array;

for (var n = 0; n < theSubPath.length - 2; n++) {theArray.push(theSubPath[0])};

myDocument.selection.select(theArray, SelectionType.REPLACE, 0, false);

layerViaCopy ("CtTL");

};

// reslect;

myDocument.activeLayer = theLayer;

app.preferences.rulerUnits = originalRulerUnits;

myDocument.resizeImage (undefined, undefined, originalResolution, ResampleMethod.NONE);

};

////// collect path info from actiondescriptor, smooth added //////

function collectPathInfoFromDesc2012 (myDocument, thePath) {

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

// based of functions from xbytor’s stdlib;

var ref = new ActionReference();

for (var l = 0; l < myDocument.pathItems.length; l++) {

          var thisPath = myDocument.pathItems;

          if (thisPath == thePath && thisPath.name == "Work Path") {

                    ref.putProperty(cTID("Path"), cTID("WrPt"));

                    };

          if (thisPath == thePath && thisPath.name != "Work Path" && thisPath.kind != PathKind.VECTORMASK) {

                    ref.putIndex(cTID("Path"), l + 1);

                    };

          if (thisPath == thePath && thisPath.kind == PathKind.VECTORMASK) {

        var idPath = charIDToTypeID( "Path" );

        var idPath = charIDToTypeID( "Path" );

        var idvectorMask = stringIDToTypeID( "vectorMask" );

        ref.putEnumerated( idPath, idPath, idvectorMask );

                    };

          };

var desc = app.executeActionGet(ref);

var pname = desc.getString(cTID('PthN'));

// create new array;

var theArray = new Array;

var pathComponents = desc.getObjectValue(cTID("PthC")).getList(sTID('pathComponents'));

// for subpathitems;

for (var m = 0; m < pathComponents.count; m++) {

          var listKey = pathComponents.getObjectValue(m).getList(sTID("subpathListKey"));

          var operation1 = pathComponents.getObjectValue(m).getEnumerationValue(sTID("shapeOperation"));

          switch (operation1) {

                    case 1097098272:

                    var operation = 1097098272 //cTID('Add ');

                    break;

                    case 1398961266:

                    var operation = 1398961266 //cTID('Sbtr');

                    break;

                    case 1231975538:

                    var operation = 1231975538 //cTID('Intr');

                    break;

                    default:

//                    case 1102:

                    var operation = sTID('xor') //ShapeOperation.SHAPEXOR;

                    break;

                    };

// for subpathitem’s count;

          for (var n = 0; n < listKey.count; n++) {

                    theArray.push(new Array);

                    var points = listKey.getObjectValue(n).getList(sTID('points'));

                    try {var closed = listKey.getObjectValue(n).getBoolean(sTID("closedSubpath"))}

                    catch (e) {var closed = false};

// for subpathitem’s segment’s number of points;

                    for (var o = 0; o < points.count; o++) {

                              var anchorObj = points.getObjectValue(o).getObjectValue(sTID("anchor"));

                              var anchor = [anchorObj.getUnitDoubleValue(sTID('horizontal')), anchorObj.getUnitDoubleValue(sTID('vertical'))];

                              var thisPoint = [anchor];

                              try {

                                        var left = points.getObjectValue(o).getObjectValue(cTID("Fwd "));

                                        var leftDirection = [left.getUnitDoubleValue(sTID('horizontal')), left.getUnitDoubleValue(sTID('vertical'))];

                                        thisPoint.push(leftDirection)

                                        }

                              catch (e) {

                                        thisPoint.push(anchor)

                                        };

                              try {

                                        var right = points.getObjectValue(o).getObjectValue(cTID("Bwd "));

                                        var rightDirection = [right.getUnitDoubleValue(sTID('horizontal')), right.getUnitDoubleValue(sTID('vertical'))];

                                        thisPoint.push(rightDirection)

                                        }

                              catch (e) {

                                        thisPoint.push(anchor)

                                        };

                              try {

                                        var smoothOr = points.getObjectValue(o).getBoolean(cTID("Smoo"));

                                        thisPoint.push(smoothOr)

                                        }

                              catch (e) {thisPoint.push(false)};

                              theArray[theArray.length - 1].push(thisPoint);

                              };

                    theArray[theArray.length - 1].push(closed);

                    theArray[theArray.length - 1].push(operation);

                    };

          };

// by xbytor, thanks to him;

function cTID (s) { return cTID || cTID = app.charIDToTypeID(s); };

function sTID (s) { return sTID || sTID = app.stringIDToTypeID(s); };

// reset;

app.preferences.rulerUnits = originalRulerUnits;

return theArray;

};

////// layer via copy, CtTL cuts,  CpTL copies //////

function layerViaCopy (aString) {

var theLayer = app.activeDocument.activeLayer;

// layer via copy;

try {

var id14 = charIDToTypeID( aString );

executeAction( id14, undefined, DialogModes.NO );

} catch (e) {};

// reselect;

app.activeDocument.activeLayer = theLayer;

};

////// determine selected path, updated 08.2011 //////

function selectedPath () {

try {

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Path"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var desc = executeActionGet(ref);

var theName = desc.getString(charIDToTypeID("PthN"));

return app.activeDocument.pathItems.getByName(theName)

}

catch (e) {

          return undefined

          }

};

////// function to get the date //////

function timeString () {

var now = new Date();

return now.getTime()

};

JJMack
Community Expert
Community Expert
March 12, 2014

First of all up front I do not know anything about Adobe AI or anything About how Adobe does pasting between Adobe applications.  If you have a real Photoshop Path and know something about individual path segments or sub-paths what you want to do may well be scriptable.  However if you know nothing about scripting Adobe products unless your a very strong programmer IMO you need to look for a Photoshop script programmer for heir that has the required knowledge.

JJMack