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

Script for exporting transparent PNG files with ICC embedded

New Here ,
Mar 05, 2025 Mar 05, 2025

Copy link to clipboard

Copied

Hi,

 

I'm trying to create a script for Photoshop that  will convert layered tiff files to png files with transparency.

I created this script, but despite having transparency = true in the save options, the pngs are saved without transparency. They are also converted to sRGB when I want the original profile to be preserved and embedded. 

 

#target photoshop
app.displayDialogs = DialogModes.NO;

function convertTIFtoPNG() {
    var inputFolder = Folder.selectDialog("Select the folder containing TIFF images");
    if (!inputFolder) return; // Exit if no folder is selected

    var outputFolder = new Folder(inputFolder + "/PNG");
    if (!outputFolder.exists) outputFolder.create();

    var tifFiles = inputFolder.getFiles("*.tif");

    if (tifFiles.length === 0) {
        alert("No TIFF files found in the selected folder.");
        return;
    }

    for (var i = 0; i < tifFiles.length; i++) {
        var file = tifFiles[i];
        if (file instanceof File) {
            try {
                open(file);
                var doc = app.activeDocument;

                app.refresh();
                if (!doc) {
                    alert("Error: Document did not open properly.");
                    continue;
                }

                // Select all layers
                selectAllLayers();

                // Merge selected layers (preserving transparency)
                doc.activeLayer = doc.artLayers[0]; // Ensure a layer is active
                doc.mergeVisibleLayers();

                // Export As PNG using "Export As"
                var pngFilePath = outputFolder.fsName + "/" + file.name.replace(/\.[^\.]+$/, ".png");
                exportAsPNG(pngFilePath);

                doc.close(SaveOptions.DONOTSAVECHANGES);

            } catch (e) {
                alert("Error processing file: " + file.name + "\n" + e);
            }
        }
    }

    alert("Conversion completed! PNG files are in the 'PNG' subfolder.");
}

// Function to manually select all layers
function selectAllLayers() {
    var doc = app.activeDocument;
    var layers = doc.artLayers;
    var sets = doc.layerSets;

    if (layers.length + sets.length < 2) return; // No need to select if only one layer

    // Deselect all layers first
    doc.activeLayer = layers[layers.length - 1];

    // Select each layer manually
    for (var i = 0; i < layers.length; i++) {
        layers[i].visible = true; // Make sure all layers are visible
        layers[i].selected = true;
    }

    for (var j = 0; j < sets.length; j++) {
        selectGroupLayers(sets[j]); // Select layers inside groups
    }
}

// Function to select layers inside groups (Layer Sets)
function selectGroupLayers(layerSet) {
    for (var i = 0; i < layerSet.artLayers.length; i++) {
        layerSet.artLayers[i].visible = true;
        layerSet.artLayers[i].selected = true;
    }

    for (var j = 0; j < layerSet.layerSets.length; j++) {
        selectGroupLayers(layerSet.layerSets[j]); // Recursively select layers in nested groups
    }
}

// Function to export PNG using "Export As"
function exportAsPNG(filePath) {
    var exportOptions = new ExportOptionsSaveForWeb();
    exportOptions.format = SaveDocumentType.PNG;
    exportOptions.PNG8 = false; // Use PNG-24 for better quality
    exportOptions.transparency = true; // Preserve transparency
    exportOptions.interlaced = false;
    exportOptions.quality = 100; // Maximum quality
    exportOptions.includeProfile = true; // Embed color profile

    app.activeDocument.exportDocument(new File(filePath), ExportType.SAVEFORWEB, exportOptions);
}

convertTIFtoPNG();

 

 

TOPICS
Actions and scripting

Views

50
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

New Here , Mar 05, 2025 Mar 05, 2025

Nevermind, I managed to find a solution:

function processTIFs() {
    var inputFolder = Folder.selectDialog("Select a folder containing TIF files");
    if (!inputFolder) return;
    
    var files = inputFolder.getFiles("*.tif");
    if (files.length === 0) {
        alert("No TIF files found in the selected folder.");
        return;
    }
    
    var outputFolder = new Folder(inputFolder + "/PNG");
    if (!outputFolder.exists) outputFolder.create();
    
    for (var i = 0; i < files.length;
...

Votes

Translate
Adobe
Community Expert ,
Mar 05, 2025 Mar 05, 2025

Copy link to clipboard

Copied

What purpose does 

 

selectAllLayers();

 

serve when you select one Layer afterwards? 

 

Please provide sample files. 

 

Edit: To embedd the original profile I suspect you will not be able to use »Save for Web«. 

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
New Here ,
Mar 05, 2025 Mar 05, 2025

Copy link to clipboard

Copied

LATEST

Nevermind, I managed to find a solution:

function processTIFs() {
    var inputFolder = Folder.selectDialog("Select a folder containing TIF files");
    if (!inputFolder) return;
    
    var files = inputFolder.getFiles("*.tif");
    if (files.length === 0) {
        alert("No TIF files found in the selected folder.");
        return;
    }
    
    var outputFolder = new Folder(inputFolder + "/PNG");
    if (!outputFolder.exists) outputFolder.create();
    
    for (var i = 0; i < files.length; i++) {
        var file = files[i];
        if (file instanceof File) {
            openAndProcessTIF(file, outputFolder);
        }
    }
    
    alert("Processing complete. PNG files saved in the PNG folder.");
}

function openAndProcessTIF(file, outputFolder) {
    var doc = app.open(file);
    
    // Convert document to Adobe RGB (1998) profile before processing
    doc.convertProfile("Adobe RGB (1998)", Intent.RELATIVECOLORIMETRIC, true, false);
    
    // Delete hidden layers
    for (var i = doc.layers.length - 1; i >= 0; i--) {
        var layer = doc.layers[i];
        if (!layer.visible) {
            layer.remove();
        }
    }
    
    // Select all remaining layers
    selectAllLayers(doc);
    
    // Merge visible layers to preserve transparency
    try {
        app.activeDocument.activeLayer.merge();
    } catch (e) {       
    }
    
    // Save as PNG-24 with embedded color profile
    try {
        var pngFile = new File(outputFolder + "/" + file.name.replace(/\.[^\.]+$/, ".png"));
        var pngOptions = new PNGSaveOptions();
        pngOptions.compression = 9; // Maximum compression
        pngOptions.interlaced = false;
        pngOptions.embedColorProfile = true; // Ensure color profile is embedded
        app.activeDocument.saveAs(pngFile, pngOptions, true, Extension.LOWERCASE);
    } catch (e) {
        alert("Error saving file: " + e);
    }
    
    // Close the document without saving changes
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

function selectAllLayers(doc) {
    var layers = doc.artLayers;
    if (layers.length > 0) {
        doc.activeLayer = layers[layers.length - 1];
        for (var i = layers.length - 2; i >= 0; i--) {
            layers[i].selected = true;
        }
    }
}

// Run the script
processTIFs();

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