How to Export Layers as PNG Files in Photoshop without Background Layer  / 背景レイヤーなしでPhotoshopでレイヤーごとにPNGァイルをエクスポートする方法

Create the Script スクリプトの作成

Open a text editor (e.g., Notepad++, Visual Studio Code) and paste the following script. This script will save all layers as PNG files.

テキストエディタ(例: Notepad++、Visual Studio Codeなど)を開き、以下のスクリプトを貼り付ける。このスクリプトは、すべてのレイヤーをPNGファイルとして保存する。

#target photoshop

var doc = app.activeDocument;
var outputFolder = Folder.selectDialog("Select output folder");

if (outputFolder) {
    var layers = doc.artLayers;
    var backgroundLayer = null;

    // Find the background layer
    for (var i = 0; i < layers.length; i++) {
        if (layers[i].isBackgroundLayer) {
            backgroundLayer = layers[i];
            break;
        }
    }

    for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];

        // Make sure all layers are hidden first
        for (var j = 0; j < layers.length; j++) {
            layers[j].visible = false;
        }

        // Make the current layer visible
        layer.visible = true;

        // Duplicate the document
        var newDoc = doc.duplicate();

        // Remove all layers except the current one and the background layer
        for (var k = newDoc.artLayers.length - 1; k >= 0; k--) {
            var artLayer = newDoc.artLayers[k];
            if (artLayer != layer && (backgroundLayer && artLayer != backgroundLayer)) {
                artLayer.remove();
            }
        }

        // Save as PNG
        var pngFile = new File(outputFolder + "/" + layer.name + ".png");
        var pngSaveOptions = new PNGSaveOptions();
        pngSaveOptions.compression = 9; // Compression level (0-9)
        newDoc.saveAs(pngFile, pngSaveOptions, true, Extension.LOWERCASE);

        // Close the duplicated document
        newDoc.close(SaveOptions.DONOTSAVECHANGES);
    }

    alert("Layer export is complete.");
}

=> Change this .txt to .jsx

Key Points of the Script

  • Find the Background Layer: Check each layer to identify if it is a background layer.
  • Hide All Layers: Hide all layers except the one currently being processed.
  • Duplicate the Document: Create a new document with only the current layer visible.
  • Remove Other Layers Except the Current Layer and Background Layer: Delete all layers except the current layer and keep the background layer if it exists.
  • Save as PNG: Save the currently visible layer as a PNG file.
  • Close the New Document: Close the duplicated document after saving is complete.

このスクリプトのポイント

  1. 背景レイヤーを見つける: 各レイヤーをチェックして、背景レイヤーかどうかを確認する。
  2. 全レイヤーを非表示にする: 現在処理中のレイヤー以外を非表示にする。
  3. ドキュメントを複製する: 現在のレイヤーだけが表示される新しいドキュメントを作成する。
  4. 現在のレイヤー以外を削除するが、背景レイヤーは削除しない: 複製したドキュメント内のレイヤーで、背景レイヤー以外のレイヤーを削除する。背景レイヤーはそのまま残す。
  5. PNGとして保存する: 現在表示中のレイヤーだけをPNGファイルとして保存する。

Save the File / ファイルとして保存:

Save the script with a .jsx extension (e.g., ExportLayers.jsx).

スクリプトを .jsx 拡張子で保存する(例: ExportLayers.jsx)。

Running the Script in Photoshop / Photoshopでのスクリプトの実行

Open Photoshop and then execute the script. Go to File > Scripts > Browse..., select the .jsx file you saved, and run it.

Photoshopを開いてスクリプトを実行する。ファイル > スクリプト > 参照... を選択し、保存した .jsx ファイルを選択して実行します。

Join the ConversationLeave a reply

Your email address will not be published. Required fields are marked *

Comment*

Name*

Website