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

Detect if canvas is empty, problem with white image.

Participant ,
May 17, 2019 May 17, 2019

Copy link to clipboard

Copied

I have compiled a script that detects if a given file is completely blank and if it is it creates a 2px x 2px black box and then trims down to it.

The issue i am having is if a file is entirely white (like a white box on a transparent canvas) the script thinks the file is empty and creates a 2px x 2px box.

Cant figure out how to make the histogram understand white, though through my understanding of histogram is it sees it as a black/white image?

#target photoshop 

var doc = activeDocument 

app.preferences.rulerUnits = Units.PIXELS; 

var tally = 0; 

var empty = false; 

 

for(var j=0;j<doc.channels.length;j++)

    var h = doc.channels.histogram; 

    for (i=0;i<h.length;i++){ 

        if(h == parseInt(doc.width*doc.height))

        {

             empty = true

               var boxFill = new SolidColor();

               boxFill.rgb.red = 0;

               boxFill.rgb.green = 0;

               boxFill.rgb.blue = 0;

               var selectionPX = [

                    [0,0],

                    [0,2],

                    [2,2],

                    [2,0]

               ]

               doc.selection.select(selectionPX);

               doc.selection.fill (boxFill);

               doc.selection.deselect ();

         } 

    } 

    if(empty)

    {

       tally++;

    } 

    empty = false; 

}  

    if (tally!=doc.channels.length)

    {

    doc.trim(TrimType.TRANSPARENT)

}; 

TOPICS
Actions and scripting

Views

585

Translate

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

correct answers 1 Correct answer

People's Champ , May 17, 2019 May 17, 2019
Blank - does this mean completely transparent?
If yes, then try.

var blank = true; 

var d = new ActionDescriptor(); 

var r = new ActionReference(); 

r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection")); 

d.putReference(stringIDToTypeID("null"), r); 

var r1 = new ActionReference(); 

r1.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("transparencyEnum")); 

r1.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDTo

...

Votes

Translate

Translate
Adobe
People's Champ ,
May 17, 2019 May 17, 2019

Copy link to clipboard

Copied

Blank - does this mean completely transparent?
If yes, then try.

var blank = true; 

var d = new ActionDescriptor(); 

var r = new ActionReference(); 

r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection")); 

d.putReference(stringIDToTypeID("null"), r); 

var r1 = new ActionReference(); 

r1.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("transparencyEnum")); 

r1.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("merged"));

d.putReference(stringIDToTypeID("to"), r1); 

try { executeAction(stringIDToTypeID("set"), d, DialogModes.NO); }  catch(e) { blank = false; } 

if (blank) try { app.activeDocument.selection.bounds; blank = false; } catch (e) { } 

app.activeDocument.selection.deselect();

alert(blank);

Votes

Translate

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
Participant ,
May 20, 2019 May 20, 2019

Copy link to clipboard

Copied

LATEST

That works!

Curious to learn more about what ActionDescriptor and ActionReference with putProperty/putReference, Any good links you could recommend?


Thanks!

Votes

Translate

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