Skip to main content
Participant
September 26, 2008
Question

Script to resize a SELECTION to specific dimensions?

  • September 26, 2008
  • 5 replies
  • 2575 views
Sounds like it ought to be easy, but heck if I can figure out how to do it. Is there a way to run a CS3 Photoshop script that will resize a SELECTION (just part of an image, not the entire image) to a specific pixel size (ex. 100 x 150)? I know you can manually resize a selection to a specific PERCENTAGE of its original size, but my script has to automatically shrink image selections of all different sizes down to a given H x W.

Any ideas?
This topic has been closed for replies.

5 replies

Paul Riggott
Inspiring
September 26, 2008
Yes that should work Mark, just being playing and this seems to work, needs a selection to be made and the layer must not be the background.
resizeSelection() is called using pixels.



resizeSelection(100,200);



/////////////////////////////////////////////////////////

//A selection must be made first

/////////////////////////////////////////////////////////

function resizeSelection(horizontal, vertical){

var strtRulerUnits = app.preferences.rulerUnits;

var strtTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

var SB = activeDocument.selection.bounds;

var Height = SB[3].value - SB[1].value;

var Width = SB[2].value - SB[0].value;

var onePixH = 100/Height;

var onePixW = 100/Width;

var newWidth = onePixW * horizontal;

var newHeight = onePixH * vertical;

activeDocument.selection.resize( newWidth , newHeight, AnchorPosition.MIDDLECENTER );

app.preferences.rulerUnits = strtRulerUnits;

app.preferences.typeUnits = strtTypeUnits;

}
Known Participant
September 26, 2008
Paul, you are quite right bad me didn't look at the OP's question properly. Sorry. If you didn't want the new doc route you could save history step, crop to selection, resize canvas, copy, return to saved history state & paste? or at least I think you could.
Paul Riggott
Inspiring
September 26, 2008
You will have the same problem Mark as resize on layers is by percentage.
activeLayer.resize( [horizontal] [, vertical] [, anchor]);
Known Participant
September 26, 2008
If you can make copy of your selection contents then you could record with ScriptListener "New Layer Via Copy" then resize & move this layer. Not tried this.
Paul Riggott
Inspiring
September 26, 2008
Not good, but you could create a new document from the selection, resize and paste back into a new selection.