Skip to main content
Participant
April 3, 2013
Answered

How can I automatically create a rectangular layer mask from an irregularly shaped selection?

  • April 3, 2013
  • 3 replies
  • 914 views

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

This topic has been closed for replies.
Correct answer c.pfaffenbichler

// 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) {}

};

3 replies

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
April 3, 2013

// 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) {}

};

sberringAuthor
Participant
April 3, 2013

This worked perfectly, thank you!

c.pfaffenbichler
Community Expert
Community Expert
April 3, 2013

You’re welcome.

c.pfaffenbichler
Community Expert
Community Expert
April 3, 2013

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().