Copy link to clipboard
Copied
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!!
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(stri
...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks for the action however it was unsuccessful. It did not maintain the aspect ratio.
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
I believe that I found the issue. I have updated to a new version, please try this new action and trash the first one:
Copy link to clipboard
Copied
Once again thanks so much for all your time and effort!!! The action works some times but not other. Look at these screen shots. In the first one it is perfect!! In the second one it does not crop down close enough to the image and it changes that aspect ratio.
Copy link to clipboard
Copied
Yes, the second image doesn't have enough white space around it to crop down to 3:2 without adding extra canvas.
I was avoiding adding artificial white space and was using the existing background as the first example had ample captured white space. This new image doesn't have enough captured white space and requires extension.
I tried the script from @r-bin and it didn't crop down the image to the top and bottom but it did extend the canvas to maintain a 3:2 ratio.
So again, I would suggest that you supply a mixture of actual images, not screenshots for adequate development/testing as a sample set of 1 or 2 may not be enough to accurately represent your varied products and variable captured white space.
Copy link to clipboard
Copied
When I use @r-bin script if I select the subject first then run the script I works flawlessly. If I don't select the subject first the script will sometimes crop in the the content slightly. But I am all about finding different ways to do this. Here are some sample images to test with.
Copy link to clipboard
Copied
Try removing the step of adding the Levels layer and then the step of removing this layer after the Trim procedure. The temporary correction was made because your background is not all white in the screenshots, artifacts are encountered. To eliminate them, temporary image correction is applied. But if the object is lighter than 128 levels, then its borders will suffer with such correction.
Copy link to clipboard
Copied
Sounds good, I will give that a try. Once again thanks!!!!
Copy link to clipboard
Copied
That removeD the need to reselect the subject! It is even better now! r-bin FOR THE WIN!
Copy link to clipboard
Copied
It's still not cropping to the top/bottom for me using your screenshot of the second image, even with the changes suggested by r-bin. Can you upload that second image?
Copy link to clipboard
Copied
Thank you for the sample images, they do help over screenshots.
I can now see why some images work and others don't. Some images are invalid for your rule of 3:2 landscape when the image is cropped top and bottom. It doesn't matter that they start 3:2 landscape, the issue is what ratio they become when cropped top and bottom...
You can see in the screenshots that some product portrait content is under a 3:2 landscape aspect ratio, so the width can be extended to 3:2 while cropping to the top and bottom of the product.
However, there are other images with landscape content widths greater than 3, which means that cropping the content would be required if you still wanted the white space cropped at the top and bottom. As you don't want the image content cropped away, the final image can't be 3:2 and also cropped at the top and bottom to the content! You have competing criteria.
This is why a simple action was inconsistent. A conditional action would be needed to use logic, processing portrait and landscape content differently.
Copy link to clipboard
Copied
HI simply you can record yourself via actions and use it from batch process...regards
Copy link to clipboard
Copied
Each part is a different shape so the crop needs to be different each time.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Show me how you trim such an object yourself?
Copy link to clipboard
Copied
I usually from within a few pixels.
Copy link to clipboard
Copied
This violates your original condition:
"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."
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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); }
Copy link to clipboard
Copied
How do I run this in photoshop?
Copy link to clipboard
Copied
NVM I figured out how to run it. I giving it a try now.
Copy link to clipboard
Copied
THIS IS MAGIC!!!!! THANKS SO MUCH!!!!