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

automate to remove image borders

Explorer ,
Jun 03, 2023 Jun 03, 2023

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.

TOPICS
Actions and scripting

Views

487
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
Community Expert ,
Jun 03, 2023 Jun 03, 2023

Copy link to clipboard

Copied

Possible start points include:

 

https://community.adobe.com/t5/bridge-discussions/bridge-or-photoshop-script-to-check-jpgs-and-move-...

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/check-the-document-for-a-margin-or-bo...

 

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.

Votes

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
Explorer ,
Jun 03, 2023 Jun 03, 2023

Copy link to clipboard

Copied

LATEST

Unfortunately this script does not work for me

Votes

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 ,
Jun 03, 2023 Jun 03, 2023

Copy link to clipboard

Copied

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

Votes

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
Explorer ,
Jun 03, 2023 Jun 03, 2023

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

Votes

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