How to automate the cropping procces of this type of images with script
Hey guys,
Wondering if I can automate the cropping of this images using a script instead of an action...
I need to crop it to 900 x 1200px full body
Thank you !

Hey guys,
Wondering if I can automate the cropping of this images using a script instead of an action...
I need to crop it to 900 x 1200px full body
Thank you !

Here is a script that does what I described in the last message. It requires a path for the subject (currently "Path 1", name editable on line 15) and at least one horizontal guide to indicate top crop. Values for result (width, height, bottom margin) can be adjusted on lines 12-14.
Code below, or download here:
https://www.marspremedia.com/software/download?asset=crop-model.zip
Also note: the script could have more error protection (perhaps I'll add some when time allows).
At this point, two possible errors are:
1. there are no horizontal guides set.
2. the subject path doesn't exist (there is none or it's named different).
If the script throws an error, first check a horizontal guide is set and the subject path name matches what the script expects to find.
// Crop Model.jsx
#target photoshop
var b = []; // cropping bounds
var doc;
var guides = [];
var i;
var isBackgroundLayer;
var layerBottom;
var path;
var visible;
// Target values for result (in pixels).
// Adjust these values as desired.
var w = 900; // width
var h = 1200; // height
var m = 50; // bottom margin
var pathSubject = "Path 1"; // crop to this path name
if (!app.documents.length) {
alert("Open a document.");
return;
}
doc = app.activeDocument;
// Create array of horizontal guides, sorted from top to bottom.
for (i = 0; i < doc.guides.length; i++) {
if (doc.guides[i].direction === Direction.HORIZONTAL) {
guides.push(parseInt(doc.guides[i].coordinate, 10));
}
}
guides.sort(function (a, b) {
return (a > b) ? 1 : ((a < b) ? -1 : 0);
});
app.preferences.rulerUnits = Units.PIXELS;
// Ensure bottom layer is a layer, not background.
// (to preserve pixels while resizing canvas)
//
// Get bottom layer.
layerBottom = doc.layers[doc.layers.length - 1];
// Preserve 'visible' property to reset when done.
visible = layerBottom.visible;
// Preserve 'isBackgroundLayer' property to reset when done.
isBackgroundLayer = layerBottom.isBackgroundLayer;
// Make bottom layer a layer, not background.
layerBottom.isBackgroundLayer = false;
// SET BOUNDS OF DESIRED CROPPING
// Get subject path.
path = doc.pathItems.getByName(pathSubject);
// Make selection of subject path.
path.makeSelection(0, true, SelectionType.REPLACE);
// Copy selection bounds to b (cropping bounds).
b = doc.selection.bounds;
// Update top bounds to position of highest guide.
b[1] = guides[0];
// Deselect.
doc.selection.deselect();
// 'b' array now set to desired cropping.
// ( array = [left, top, right, bottom] )
// CROP IMAGE
// Using resize canvas to preserve pixels outside bounds.
//
// Resize canvas to crop out above first horizontal guide.
doc.resizeCanvas(null, doc.height - b[1], AnchorPosition.BOTTOMCENTER);
// Resize canvas to crop out below subject.
doc.resizeCanvas(null, b[3] - b[1], AnchorPosition.TOPCENTER);
// Resize canvas to crop out left of subject.
doc.resizeCanvas(doc.width - b[0], null, AnchorPosition.MIDDLERIGHT);
// Resize canvas to crop out right of subject.
doc.resizeCanvas(b[2] - b[0], null, AnchorPosition.MIDDLELEFT);
// RESIZE IMAGE
// Target height less bottom margin.
doc.resizeImage(null, h - m, null, ResampleMethod.AUTOMATIC);
// Resize canvas height to add bottom margin.
// At same time resize width to center subject.
doc.resizeCanvas(w, h, AnchorPosition.TOPCENTER);
// Restore original bottom layer properties.
layerBottom.isBackgroundLayer = isBackgroundLayer;
layerBottom.visible = visible;
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.