Skip to main content
natrev
Legend
January 26, 2017
Question

Help Needed! Crop and Padding with script

  • January 26, 2017
  • 2 replies
  • 1027 views

Hi Everyone!

I have one Photoshop document 3600Px (W) X 3600px (H) x 300 dpi which contain one product with overall photoshop path.

Now I get a selection from the path & expand 50px and crop the image 1760x1760x300.

Subsequently, I check the padding was not 50 px from the edge of the product.

If suppose I expand 40px and crop the image. the image padding 50px around the image.

How do I get exact expand pixel value? Please suggest me.

-yajiv

Code:

if ( app.documents.length <= 0 ) alert( strAlertDocumentMustBeOpened,"Smart Size" );

else{  main(); }

function main(){

    app.preferences.rulerUnits = Units.PIXELS;

    app.preferences.typeUnits = TypeUnits.PIXELS;

    app.displayDialogs = DialogModes.NO;

    var docRef = app.activeDocument;

    var dname = docRef.name;

    var fileName=dname.replace(/\.[^\.]+$/, '');

    var filePath=activeDocument.path;

    var myLayers=docRef.layers      

    docRef.pathItems[0].makeSelection(0,true,SelectionType.REPLACE);

    docRef.selection.expand(50)

    docRef.crop(docRef.selection.bounds);       

    var imgW=docRef.width;

    var imgH=docRef.height;

    if(imgW>imgH){

        docRef.resizeImage(new UnitValue(1760,'Px'), undefined, 300, ResampleMethod.AUTOMATIC);

        docRef.resizeCanvas( undefined,new UnitValue(1760,'Px'),AnchorPosition.MIDDLECENTER);

    }else{

        docRef.resizeImage(undefined, new UnitValue(1760,'Px'),300, ResampleMethod.AUTOMATIC);

        docRef.resizeCanvas( new UnitValue(1760,'Px'),undefined,AnchorPosition.MIDDLECENTER);

    }

}

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
January 29, 2017

Why not use the Selection’s bounds as the basis of a calculation instead of expanding the Selection?

If you resample the image after expanding the Selection is seems hardly surprising that the original 50px »frame« can change, so you may want to first resample, then create the Selection etc.

SuperMerlin
Inspiring
January 26, 2017

Would this work?

#target photoshop;

app.bringToFront();

main();

function main(){

if(!documents.length) return;

try{

var doc = app.activeDocument;

app.displayDialogs = DialogModes.NO;

var strtRulerUnits = app.preferences.rulerUnits;

var strtTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

if(doc.pathItems.length){

doc.pathItems[0].makeSelection(0,true,SelectionType.REPLACE);

app.activeDocument.selection.expand(50);

executeAction( charIDToTypeID( "Crop" ), undefined, DialogModes.NO );

var Max = Math.max(doc.width.value,doc.height.value);

app.activeDocument.resizeCanvas(Max, Max, AnchorPosition.MIDDLECENTER);

doc.resizeImage( undefined,1760, 300, ResampleMethod.AUTOMATIC);

}

}catch(e){

}

finally{

app.preferences.rulerUnits = strtRulerUnits;

app.preferences.typeUnits = strtTypeUnits;

};

};

natrev
natrevAuthor
Legend
January 26, 2017

Hi Merlin,

Thank you for the prompt response.

Still I get same problem and the image padding size less than 50px.

Please advice.

-yajiv

SuperMerlin
Inspiring
January 26, 2017

Have you an example file I could test with ?