Copy link to clipboard
Copied
I am trying to automatically crop images. The subject is on a white background and extends from the left edge of the image to the right. The top and bottom of the subject are within the frame. I need to crop a certain distance from the top and bottom of the subject and a predefined width that would be calculated from the height of the subject.
My current idea is to create a duplicate layer and run Threshold on it, which allows me to select the subject clearly. Once I have the subject selected I can use Image -> Crop to create the desired rectangle, but I can't figure out a way to create a mask from the irregularly selected subject. Does anyone know a way to take a selection and convert it to a rectangle? I hope this makes sense, I can try to explain better if not. Thanks.
--Chris
// make selection rectangular;
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theBounds = myDocument.selection.bounds;
var theArray = [[theBounds[0], theBounds[1]], [theBounds[2], theBounds[1]], [theBounds[2], theBounds[3]], [theBounds[0], theBounds[3]]];
myDocument.selection.select(theArray, SelectionType.REPLACE, 0, false);
}
catch (e) {}
};
Copy link to clipboard
Copied
Even an irregularly shaped Selection has rectangular bounds.
And those values can be used to define the four corners of a rectangular selection with Selection.select().
Copy link to clipboard
Copied
// make selection rectangular;
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theBounds = myDocument.selection.bounds;
var theArray = [[theBounds[0], theBounds[1]], [theBounds[2], theBounds[1]], [theBounds[2], theBounds[3]], [theBounds[0], theBounds[3]]];
myDocument.selection.select(theArray, SelectionType.REPLACE, 0, false);
}
catch (e) {}
};
Copy link to clipboard
Copied
This worked perfectly, thank you!
Copy link to clipboard
Copied
You’re welcome.
Copy link to clipboard
Copied