[PS CC2025] Как автоматизировать

Pavel Karpov

Топикстартер
15 лет на форуме
Сообщения
90
Реакции
17
Есть большая куча изображений — vasya.tif, petya.tif и т.д. и к ней вторая куча — vasya_denoise.tif, petya_denoise.tif и т.д. Задача: сделать файл с двумя слоями из которых нижний слой исходный, а верхний, взятый из одноименного файла с припиской _denoise. Ручками скучно, файлов несколько сотен....
 
пока LLM в чате не забанили...

JavaScript:
// Script for combining original and denoised images in Photoshop
// This script takes original TIF files and their denoised counterparts
// and creates a new file with two layers: original (bottom) and denoised (top)

// User dialog to select source folders
function main() {
    // Create dialog for selecting folders
    var dialog = new Window("dialog", "Combine Original and Denoised Images");
    
    dialog.orientation = "column";
    dialog.alignChildren = "left";
    
    // Group for original files
    var origGroup = dialog.add("group");
    origGroup.orientation = "row";
    origGroup.alignChildren = "left";
    origGroup.add("statictext", undefined, "Original Images Folder:");
    var origFolderEdit = origGroup.add("edittext", undefined, "");
    origFolderEdit.preferredSize.width = 300;
    var origBrowseBtn = origGroup.add("button", undefined, "Browse...");
    
    // Group for denoised files
    var denoiseGroup = dialog.add("group");
    denoiseGroup.orientation = "row";
    denoiseGroup.alignChildren = "left";
    denoiseGroup.add("statictext", undefined, "Denoised Images Folder:");
    var denoiseFolderEdit = denoiseGroup.add("edittext", undefined, "");
    denoiseFolderEdit.preferredSize.width = 300;
    var denoiseBrowseBtn = denoiseGroup.add("button", undefined, "Browse...");
    
    // Group for output folder
    var outputGroup = dialog.add("group");
    outputGroup.orientation = "row";
    outputGroup.alignChildren = "left";
    outputGroup.add("statictext", undefined, "Output Folder:");
    var outputFolderEdit = outputGroup.add("edittext", undefined, "");
    outputFolderEdit.preferredSize.width = 300;
    var outputBrowseBtn = outputGroup.add("button", undefined, "Browse...");
    
    // Buttons
    var btnGroup = dialog.add("group");
    btnGroup.orientation = "row";
    btnGroup.alignment = "center";
    var okBtn = btnGroup.add("button", undefined, "OK", {name: "ok"});
    var cancelBtn = btnGroup.add("button", undefined, "Cancel", {name: "cancel"});
    
    // Browse button handling
    origBrowseBtn.onClick = function() {
        var folder = Folder.selectDialog("Select Original Images Folder");
        if (folder) {
            origFolderEdit.text = folder.fsName;
        }
    };
    
    denoiseBrowseBtn.onClick = function() {
        var folder = Folder.selectDialog("Select Denoised Images Folder");
        if (folder) {
            denoiseFolderEdit.text = folder.fsName;
        }
    };
    
    outputBrowseBtn.onClick = function() {
        var folder = Folder.selectDialog("Select Output Folder");
        if (folder) {
            outputFolderEdit.text = folder.fsName;
        }
    };
    
    // Show dialog
    if (dialog.show() == 1) {
        // Process folders if OK is clicked
        processImages(origFolderEdit.text, denoiseFolderEdit.text, outputFolderEdit.text);
    }
}

// Process images in the selected folders
function processImages(origFolderPath, denoiseFolderPath, outputFolderPath) {
    // Get folder objects
    var origFolder = new Folder(origFolderPath);
    var denoiseFolder = new Folder(denoiseFolderPath);
    var outputFolder = new Folder(outputFolderPath);
    
    // Create output folder if it doesn't exist
    if (!outputFolder.exists) {
        outputFolder.create();
    }
    
    // Get all .tif files from original folder
    var origFiles = origFolder.getFiles("*.tif");
    
    // Process each original file
    for (var i = 0; i < origFiles.length; i++) {
        var origFile = origFiles[i];
        var filename = origFile.name;
        var baseName = filename.substring(0, filename.lastIndexOf("."));
        
        // Look for corresponding denoised file
        var denoiseFile = new File(denoiseFolder + "/" + baseName + "_denoise.tif");
        
        if (denoiseFile.exists) {
            // Create new file with both layers
            combineImages(origFile, denoiseFile, outputFolder + "/" + baseName + "_combined.psd");
        } else {
            alert("Denoised file not found for: " + filename);
        }
    }
    
    alert("Processing complete!");
}

// Combine original and denoised images into a single file with layers
function combineImages(origFile, denoiseFile, outputPath) {
    // Open the original file
    var origDoc = open(origFile);
    
    // Get document dimensions
    var docWidth = origDoc.width;
    var docHeight = origDoc.height;
    
    // Duplicate the document to create a new PSD
    var newDoc = origDoc.duplicate();
    
    // Close the original document
    origDoc.close(SaveOptions.DONOTSAVECHANGES);
    
    // Open the denoised file
    var denoiseDoc = open(denoiseFile);
    
    // Select all and copy
    denoiseDoc.selection.selectAll();
    denoiseDoc.selection.copy();
    
    // Close the denoised document
    denoiseDoc.close(SaveOptions.DONOTSAVECHANGES);
    
    // Paste the denoised image into the new document
    newDoc.paste();
    
    // Rename layers
    newDoc.layers[0].name = "Denoised";  // Top layer
    newDoc.layers[1].name = "Original";  // Bottom layer
    
    // Save the new document
    var saveOptions = new PhotoshopSaveOptions();
    saveOptions.layers = true;  // Preserve layers
    saveOptions.embedColorProfile = true;
    
    newDoc.saveAs(new File(outputPath), saveOptions, true, Extension.LOWERCASE);
    
    // Close the new document
    newDoc.close(SaveOptions.DONOTSAVECHANGES);
}

// Run the script
main();
 
Работает как часы 'dans))'