Skip to main content
Participating Frequently
March 19, 2024
Answered

Is it possible to trim(not crop) while maintaining aspect ratio.

  • March 19, 2024
  • 3 replies
  • 2095 views

I have images that are 3 by 2 aspect ratio.  The images are of products.  I am removing the backgrounds and then replacing them with pure white.  What I would like to be able to do automatically is trim the edges of the images down to the top and bottom edges of the products but keep the aspect ratio.   I know I can crop down to the products manually but I have many many products to do and every little bit of time savings is important.  Thanks in advance for your help!!

 

This topic has been closed for replies.
Correct answer r-bin

Sorry for the confusion.  I want to crop down to the content of the image within a few pixels.  If the product is taller that it is wide it will be with in a few pixels of the top and bottom, if the product is wider than it is tall this will put the crop with in a few pixels of the left and right.  


try this script

try {

// add levels

var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("adjustmentLayer"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
var d2 = new ActionDescriptor();
d2.putEnumerated(stringIDToTypeID("presetKind"), stringIDToTypeID("presetKindType"), stringIDToTypeID("presetKindDefault"));
var list = new ActionList();
var d3 = new ActionDescriptor();
var r1 = new ActionReference();
r1.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("composite"));
d3.putReference(stringIDToTypeID("channel"), r1);
var list1 = new ActionList();
list1.putInteger(0);
list1.putInteger(128);
d3.putList(stringIDToTypeID("input"), list1);
list.putObject(stringIDToTypeID("levelsAdjustment"), d3);
d2.putList(stringIDToTypeID("adjustment"), list);
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("levels"), d2);
d.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

// trim

var d = new ActionDescriptor();
d.putEnumerated(stringIDToTypeID("trimBasedOn"), stringIDToTypeID("trimBasedOn"), stringIDToTypeID("topLeftPixelColor"));
d.putBoolean(stringIDToTypeID("top"), true);
d.putBoolean(stringIDToTypeID("bottom"), true);
d.putBoolean(stringIDToTypeID("left"), true);
d.putBoolean(stringIDToTypeID("right"), true);
executeAction(stringIDToTypeID("trim"), d, DialogModes.NO);

// delete levels layer

var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
executeAction(stringIDToTypeID("delete"), d, DialogModes.NO);

// expand canvas +3px, white color

var d = new ActionDescriptor();
d.putBoolean(stringIDToTypeID("relative"), true);
d.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("pixelsUnit"), 6);
d.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("pixelsUnit"), 6);
d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);

app.preferences.rulerUnits = Units.PIXELS;

var w = activeDocument.width.value;
var h = activeDocument.height.value;

if (w/h > 1.5)
    {
    var d = new ActionDescriptor();
    d.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("pixelsUnit"), Math.round(w/1.5));
    d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
    executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);
    }
else    
    {
    var d = new ActionDescriptor();
    d.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("pixelsUnit"), Math.round(h*1.5));
    d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
    executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);
    }


} catch (e) { alert(e.line+ "\n\n" +e); }

3 replies

Legend
March 20, 2024

Show me how you trim such an object yourself?

 

 

 

Participating Frequently
March 20, 2024

I usually from within a few pixels. 

r-binCorrect answer
Legend
March 20, 2024

Sorry for the confusion.  I want to crop down to the content of the image within a few pixels.  If the product is taller that it is wide it will be with in a few pixels of the top and bottom, if the product is wider than it is tall this will put the crop with in a few pixels of the left and right.  


try this script

try {

// add levels

var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("adjustmentLayer"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
var d2 = new ActionDescriptor();
d2.putEnumerated(stringIDToTypeID("presetKind"), stringIDToTypeID("presetKindType"), stringIDToTypeID("presetKindDefault"));
var list = new ActionList();
var d3 = new ActionDescriptor();
var r1 = new ActionReference();
r1.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("composite"));
d3.putReference(stringIDToTypeID("channel"), r1);
var list1 = new ActionList();
list1.putInteger(0);
list1.putInteger(128);
d3.putList(stringIDToTypeID("input"), list1);
list.putObject(stringIDToTypeID("levelsAdjustment"), d3);
d2.putList(stringIDToTypeID("adjustment"), list);
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("levels"), d2);
d.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

// trim

var d = new ActionDescriptor();
d.putEnumerated(stringIDToTypeID("trimBasedOn"), stringIDToTypeID("trimBasedOn"), stringIDToTypeID("topLeftPixelColor"));
d.putBoolean(stringIDToTypeID("top"), true);
d.putBoolean(stringIDToTypeID("bottom"), true);
d.putBoolean(stringIDToTypeID("left"), true);
d.putBoolean(stringIDToTypeID("right"), true);
executeAction(stringIDToTypeID("trim"), d, DialogModes.NO);

// delete levels layer

var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
executeAction(stringIDToTypeID("delete"), d, DialogModes.NO);

// expand canvas +3px, white color

var d = new ActionDescriptor();
d.putBoolean(stringIDToTypeID("relative"), true);
d.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("pixelsUnit"), 6);
d.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("pixelsUnit"), 6);
d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);

app.preferences.rulerUnits = Units.PIXELS;

var w = activeDocument.width.value;
var h = activeDocument.height.value;

if (w/h > 1.5)
    {
    var d = new ActionDescriptor();
    d.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("pixelsUnit"), Math.round(w/1.5));
    d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
    executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);
    }
else    
    {
    var d = new ActionDescriptor();
    d.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("pixelsUnit"), Math.round(h*1.5));
    d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
    d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
    executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);
    }


} catch (e) { alert(e.line+ "\n\n" +e); }
lambiloon
Community Expert
Community Expert
March 19, 2024

HI simply you can record yourself via actions and use it from batch process...regards

Ali Sajjad / Graphic Design Trainer / Freelancer / Adobe Certified Professional
Participating Frequently
March 19, 2024

Each part is a different shape so the crop needs to be different each time.  

 

Stephen Marsh
Community Expert
Community Expert
March 20, 2024
quote

Each part is a different shape so the crop needs to be different each time.  

 


By @Daniel35488851bybz

 

An appropriately composed action can account for the variable nature of each image.

Stephen Marsh
Community Expert
Community Expert
March 19, 2024

It's hard to test from a single screenshot, however, you can try this action:

 

<original content deleted due to an error>

 

You can use the Automate > Batch command to run this action over multiple files.

Participating Frequently
March 19, 2024

Thanks for the action however it was unsuccessful.  It did not maintain the aspect ratio.  

Stephen Marsh
Community Expert
Community Expert
March 20, 2024

@Daniel35488851bybz wrote:

Thanks for the action however it was unsuccessful.  It did not maintain the aspect ratio.  


 

It did in my test...

 

EDIT: I have now revised the original action and uploaded a new link.