Skip to main content
Known Participant
June 3, 2023
Question

automate to remove image borders

  • June 3, 2023
  • 2 replies
  • 938 views

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.

This topic has been closed for replies.

2 replies

Jeff Arola
Community Expert
Community Expert
June 3, 2023

Have you tried File>Automate>Crop and Straighten Photos?

raeenAuthor
Known Participant
June 4, 2023

It works with rasterized image only but most of the time it works perfectly fine. Thank you so much for helping me in this

Stephen Marsh
Community Expert
Community Expert
June 3, 2023

Possible start points include:

 

https://community.adobe.com/t5/bridge-discussions/bridge-or-photoshop-script-to-check-jpgs-and-move-the-jpgs-with-a-white-border-to-another-folder/m-p/12155947

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/check-the-document-for-a-margin-or-border/m-p/12563008

 

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.

raeenAuthor
Known Participant
June 4, 2023

Unfortunately this script does not work for me