Skip to main content
toddf56888254
Participant
April 3, 2018
Question

Script or Action to Batch Determine Transparency?

  • April 3, 2018
  • 6 replies
  • 8408 views

Is there a script or action that could batch a folder of images and determine which files have transparency and which do not?

These are layered product shots. In some files a white bottom fill layer is turned on beneath the shadow layer. In some files it is not on, or not present, and these would be the images with transparency. However, that white fill layer is sometimes an empty layer filled with white, sometimes a solid color adjustment layer, and not consistently named, so I don't thin one could simply check for the presence of that layer.

My goal is to know which files have the transparency without having to open each one to find out. There are hundreds of them, and they are heavy files.

PS CC, Mac, sRGB

As I write this I have a thought. I suppose I could duplicate the files, then run an action on the dupes to place a solid fill layer at the bottom (of a color other than while - say, red) and via the finder or bridge see which ones have a red background and which ones don't.

Any better ideas? Would really prefer not to work on proxy files requiring me to cross reference. Would be cool if there were a way to have the action/script separate into folders, or change metadata, or something to make an easy sort job of those that do and don't have transparency.

This topic has been closed for replies.

6 replies

Stephen Marsh
Community Expert
Community Expert
April 4, 2018

There are many possibilities using an ExifTool conditional based around transparency. Command line code examples for the Mac OS follows (Windows users would simply swap around the double/single straight quotes and use appropriate file paths). As these commands overwrite the original files without making any backups, work on copies and or use at your own risk.

To recursively move files matching the if condition to a new folder titled Transparent Images in all sub folders under the top-level input folder on the desktop of the user Bob:

exiftool -overwrite_original -r -if '$AlphaChannelsNames eq "Transparency"' -directory=%d'Transparent Images' '/Users/bob/Desktop/top level input folder'

Alternatively, to recursively apply metadata such as a Bridge label of Review in all folders/files under the top-level input folder on the desktop of the user Bob:

exiftool -overwrite_original -r -if '$AlphaChannelsNames eq "Transparency"' -label='Review' '/Users/bob/Desktop/top level input folder'

Or to recursively apply metadata such as a Bridge rating of five stars in all folders/files under the top-level input folder on the desktop of the user Bob:

exiftool -overwrite_original -r -if '$AlphaChannelsNames eq "Transparency"' -rating='5' '/Users/bob/Desktop/top level input folder'

Inspiring
April 4, 2018

This is amazing work, Stephen. Does it work on closed files and/or can it be batched?

Can someone help a noob and tell me where to place this code and how to run it? What file, what folder, what program, etc?

FWIW, I'm probably most interested in option #1, moving to folders, but any of the three should be fine.

Stephen Marsh
Community Expert
Community Expert
April 4, 2018

Download and install ExifTool from the link provided in my previous post.

 

Open Terminal.app and type/paste in the following code (ensure that straight quotes don’t become curly):

 

exiftool -r -if '$AlphaChannelsNames eq "Transparency"' -directory=%d'Transparent Images'

 

Then type a word space…

 

Finally, drag the parent/top level folder from the Finder into the Terminal.app window to populate the path to the folder.

 

Press enter/return to run.

 

The command will batch process all files and folders under the top level folder. It will create a new folder within each folder titled “Transparent Images” and move all files with transparency into this folder.

 

Further commands can be added to only process specific file types, such as  -ext .psd or to ignore specific named sub-folder paths.

 

Installing ExifTool

Getting started: Command-line ExifTool in Windows

Running ExifTool

 

(it is also pretty much the same for the Mac OS)

 

Jarda Bereza
Inspiring
April 3, 2018

You could try script for Bridge. It does not open document and it read image pixels.

$.hiresTimer

#target bridge 

$.memCache = 20000000;

$.gc();

var fle = new File(Folder.desktop + '/sample-file.psd' );

reading(new BitmapData(fle, true)) 

 

function reading(v) { 

height = v.height;

width = v.width;

j = 0;

i = 0;

r = new Array(height);

c = new Array(width);

     for(; j < height; j++) {

          for(i=0; i < width; i++) { 

               c = new Color(v.getPixel(i, j)).toString();

          } 

//c = (' '+c.toString());

r = c.slice();

if(j==1){

if(r[0]===r[1]){

alert("same");

}

}

     } 

     var txt = File(fle.toString().slice(0, -3) + 'txt');

txt.open('w'); 

     txt.write(r.toSource());

txt.close() 

}

$.writeln($.hiresTimer/1000 + " ms");

This reads sample-file.psd inside desktop folder and saves data as .txt file. So you would modify input/output files.

Data are in #AARRGGBB format so you need only first two numbers.

I strongly recommend stop "for" loop when you find first transparent pixel and check next image.

Legend
April 3, 2018

Not opening the file is unlikely to succeed. If you open it, you can use a script to determine if there is transparency.

var transp = true;

var d = new ActionDescriptor();

var r = new ActionReference();

r.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );

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

var r2 = new ActionReference();

r2.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );

r2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Mrgd" ) );

d.putReference( charIDToTypeID( "T   " ), r2 );

try {

    executeAction( charIDToTypeID( "setd" ), d, DialogModes.NO );

    activeDocument.selection.invert();

    try { activeDocument.selection.bounds; } catch(e) { transp = false; }

    }

catch(e) { transp = false; }

activeDocument.selection.deselect();

if (transp) alert("Has transparency");

else        alert("No transparency");

Stephen Marsh
Community Expert
Community Expert
April 5, 2018

Although I can’t script, I can sometimes hack them in a Dr Frankenstein fashion… The following code modified from  r-bin​ (post #4) will add a description metadata value of “Transparent” which can be used in say Adobe Bridge to isolate the images using the Find command. The script can be recorded into an action and used in Automate/Batch or Image Processor to batch apply the metadata to the images. Note that any existing description metadata entry will be overwritten, with a little more work I’m sure the value could be appended or a different field such as subject/keywords could be used.

More on installing scripts here:

Prepression: Downloading and Installing Adobe Scripts

var doc = activeDocument;

var transp = true; 

var d = new ActionDescriptor(); 

var r = new ActionReference(); 

r.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) ); 

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

 

var r2 = new ActionReference(); 

r2.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) ); 

r2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Mrgd" ) );  

 

d.putReference( charIDToTypeID( "T   " ), r2 ); 

try {  

    executeAction( charIDToTypeID( "setd" ), d, DialogModes.NO );  

 

    activeDocument.selection.invert(); 

 

    try { activeDocument.selection.bounds; } catch(e) { transp = false; } 

    }  

catch(e) { transp = false; } 

 

activeDocument.selection.deselect(); 

 

if (transp) doc.info.caption += "Transparent"

Although I removed the “else” line (is there a null option?) – no errors were generated on the files that did not contain transparency. Or one could of course add a description value of “White BG” or something for the else condition.

Legend
April 5, 2018

I do not quite understand your English.)

Error

executeAction (charIDToTypeID ("setd"), d, DialogModes.NO);


will be, when there is only Background.

P.S. My English is a Google translator )

Jarda Bereza
Inspiring
April 3, 2018

If you saved PSD with maximum compatibility then you can hold left Alt + Shift and you can load composite data instead layers. This will open file much much faster. But it seems to be wired with keys and I don't know how to script it. So you could use brick as workaround :-D

In channels class is Alpha Channel for transparent pixels. It has index -1 in action reference. But values doesn't makes sense to me.

Alternatively you can load layer transparency into selection, then switch into quick mask mode and read histogram for quick mask channels. It shows me correct values.

Or there is possibility to build PSD binary parser. Composite data are last block in PSD: Adobe Photoshop File Formats Specification

This is fastest solution because you read only data you need and you don't need open PSD file. But it is most difficult.

Akash Sharma
Legend
April 3, 2018

Moving to Photoshop Scripting

c.pfaffenbichler
Community Expert
Community Expert
April 3, 2018

You may want to do a search over at

Photoshop Scripting