Copy link to clipboard
Copied
I have a collection of pictures with borders, primarily white borders. Removing these borders individually is a time-consuming task, so I'm seeking assistance to automate the process.
Is there anyone who can help me automate the removal of these borders using scripts or actions? Thank you in advance.
For reference, I have attached two pictures: one with the border (before version) and another without the border (after version).
PS: The border width in the images is not consistent. Some images have broad borders while others don't. Some images may contain numbers on white border.
Copy link to clipboard
Copied
Possible start points include:
I didn't document this one back in 2021, but it looks like I mashed up the code from another topic:
whiteBorderCheck();
function whiteBorderCheck() {
// Capture original ruler units and set ruler units to pixels
var origUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var doc = app.activeDocument;
doc.selection.selectAll();
doc.selection.selectBorder(1);
// =======================================================
var idset = stringIDToTypeID("set");
var desc990 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref239 = new ActionReference();
var idproperty = stringIDToTypeID("property");
var idquickMask = stringIDToTypeID("quickMask");
ref239.putProperty(idproperty, idquickMask);
var iddocument = stringIDToTypeID("document");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
ref239.putEnumerated(iddocument, idordinal, idtargetEnum);
desc990.putReference(idnull, ref239);
executeAction(idset, desc990, DialogModes.NO);
// =======================================================
var idthresholdClassEvent = stringIDToTypeID("thresholdClassEvent");
var desc1004 = new ActionDescriptor();
var idlevel = stringIDToTypeID("level");
desc1004.putInteger(idlevel, 128);
executeAction(idthresholdClassEvent, desc1004, DialogModes.NO);
// =======================================================
var idclearEvent = stringIDToTypeID("clearEvent");
var desc1007 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref246 = new ActionReference();
var idproperty = stringIDToTypeID("property");
var idquickMask = stringIDToTypeID("quickMask");
ref246.putProperty(idproperty, idquickMask);
var iddocument = stringIDToTypeID("document");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
ref246.putEnumerated(iddocument, idordinal, idtargetEnum);
desc1007.putReference(idnull, ref246);
executeAction(idclearEvent, desc1007, DialogModes.NO);
// https://community.adobe.com/t5/photoshop/how-do-you-get-histogram-mean-value/m-p/3857869
var histo = doc.histogram;
var mean = 0;
var total = 0;
for (var n = 0; n < histo.length; n++) {
total = total + histo[n];
};
for (var m = 0; m < histo.length; m++) {
var thisValue = histo[m];
mean = mean + (m * thisValue / total);
};
if (mean === 255) {
alert("white");
//alert(histo);
} else {
alert("not white")
//alert(histo);
}
// Return the original ruler units
app.preferences.rulerUnits = origUnits;
}
Keep in mind that pure white may not always be pure in images, particularly if they were saved with JPEG compression.
Copy link to clipboard
Copied
Unfortunately this script does not work for me
Copy link to clipboard
Copied
Have you tried File>Automate>Crop and Straighten Photos?
Copy link to clipboard
Copied
It works with rasterized image only but most of the time it works perfectly fine. Thank you so much for helping me in this