• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help Needed! Crop and Padding with script

Engaged ,
Jan 25, 2017 Jan 25, 2017

Copy link to clipboard

Copied

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);

    }

}

TOPICS
Actions and scripting

Views

842

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guide ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

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;

};

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

Hi Merlin,

Thank you for the prompt response.

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

Please advice.

-yajiv

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

Have you an example file I could test with ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2017 Jan 29, 2017

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines