Copy link to clipboard
Copied
Copy link to clipboard
Copied
AFAIK, there is no script to detect an object - at least with CS6. What might work, and only if your bg is fairly consistent in color and tone, and you have good separation between your bg and subject is to create a script that uses the magic wand to select a corner of the image, outside of the subject. This should select the bg. Then invert the selection, and get the bounds of the selection. Once you do that, you can add your 100px and crop from that.
Copy link to clipboard
Copied
You can try this:
#target photoshop
var doc = activeDocument;
sampleCorner ();
invertSel ();
var sBounds = doc.selection.bounds;
cropImg (sBounds[1]-100, sBounds[0]-100, sBounds[3]+100, sBounds[2]+100)
function sampleCorner(){
var idsetd = charIDToTypeID( "setd" );
var desc62 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref8 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref8.putProperty( idChnl, idfsel );
desc62.putReference( idnull, ref8 );
var idT = charIDToTypeID( "T " );
var desc63 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc63.putUnitDouble( idHrzn, idPxl, 3.000000 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc63.putUnitDouble( idVrtc, idPxl, 3.000000 );
var idPnt = charIDToTypeID( "Pnt " );
desc62.putObject( idT, idPnt, desc63 );
var idTlrn = charIDToTypeID( "Tlrn" );
desc62.putInteger( idTlrn, 60 );
var idAntA = charIDToTypeID( "AntA" );
desc62.putBoolean( idAntA, true );
executeAction( idsetd, desc62, DialogModes.NO );
}
function invertSel(){
var idInvs = charIDToTypeID( "Invs" );
executeAction( idInvs, undefined, DialogModes.NO );
}
function cropImg(topS, leftS, bottomS, rightS){
var idCrop = charIDToTypeID( "Crop" );
var desc78 = new ActionDescriptor();
var idT = charIDToTypeID( "T " );
var desc79 = new ActionDescriptor();
var idTop = charIDToTypeID( "Top " );
var idPxl = charIDToTypeID( "#Pxl" );
desc79.putUnitDouble( idTop, idPxl, topS );
var idLeft = charIDToTypeID( "Left" );
var idPxl = charIDToTypeID( "#Pxl" );
desc79.putUnitDouble( idLeft, idPxl, leftS );
var idBtom = charIDToTypeID( "Btom" );
var idPxl = charIDToTypeID( "#Pxl" );
desc79.putUnitDouble( idBtom, idPxl, bottomS );
var idRght = charIDToTypeID( "Rght" );
var idPxl = charIDToTypeID( "#Pxl" );
desc79.putUnitDouble( idRght, idPxl, rightS );
var idRctn = charIDToTypeID( "Rctn" );
desc78.putObject( idT, idRctn, desc79 );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc78.putUnitDouble( idAngl, idAng, 0.000000 );
var idDlt = charIDToTypeID( "Dlt " );
desc78.putBoolean( idDlt, false );
var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );
var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );
var idpureAspectRatio = stringIDToTypeID( "pureAspectRatio" );
desc78.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idpureAspectRatio );
var idCnsP = charIDToTypeID( "CnsP" );
desc78.putBoolean( idCnsP, false );
executeAction( idCrop, desc78, DialogModes.NO );
}
Copy link to clipboard
Copied
Thank you for the reply!
The script produces a small square of the background which may be due to my backgrounds. I tried a image of a object on a crude green screen (neon green poster board) to create a very high contract between the object and background but that resulted in a small green square after running the script
I previous created a App in Realbasic (Real Studio, Xojo) to do something similar several years ago. I had a series of small jpeg images of black text strings on a white background. I only wanted the images to big as the actual visible text and not include any white space around the edges. This program scanned each pixel in the image and kept track of the top most, bottom most, farthest left, and farthest right pixel that was not pure white. These four properties were then used as the crop marks.
Moving to a background other than a single rgb value is going to be a a lot more difficult as one would need to create a acceptable color range for the background color.
Copy link to clipboard
Copied
Yea, you need to have a pretty uniform bg for that script to work you could try upping the tolerance for the magic wand to 100, in line 29, but it's already at 60, which is pretty high.