Export each layer as a file in Photoshop with a script
With a set of burst shots, you can make a timelapse movie for some fast-moving objects, for example, wild animals. However, I normally shot this kind of phots handheld, so that an alignment is necessary before encoding the timelapse. Personally, I use Adobe Photoshop for this purpose. The basic workflow is like this:
- Load all image files into a stack
- Auto-align layers
- Trim the edges with transparent pixels
- Save layers back into files
- Encoding the movie with
ffmpeg
It is rather tedious if you want to do step 3 manually. As step 1 is done
through Photoshop’s built-in scripts, I was wondering if step 3 could be done
with an user script. I apparently have no knowledge at all in ExtendScript
or
the Photoshop scripting environment, so I wrote this script with the help of
ChatGPT. Of course, I had to some debugging after the code was generated by
ChatGPT, but I should give most credits to it.
Here is the script.
#target photoshop
// Ensure there is an active document open
if (app.documents.length > 0) {
var doc = app.activeDocument;
// Function to export layer as a file
function exportLayer(layer, outputFolder, saveOptions) {
// Make the target layer visible
layer.visible = true;
// Save file
var file = new File(outputFolder + "/" + layer.name);
doc.saveAs(file, saveOptions, true, Extension.LOWERCASE);
// Close the temporary document without saving
layer.visible = false;
}
// Create a dialog to select file type
function selectFileType() {
var dlg = new Window('dialog', 'Select File Type');
dlg.orientation = 'column';
dlg.alignChildren = ['left', 'center'];
dlg.add('statictext', undefined, 'Choose a file type for export:');
var fileTypeDropdown = dlg.add('dropdownlist', undefined, ['JPEG', 'PNG']);
fileTypeDropdown.selection = 0; // Default to JPEG
var okButton = dlg.add('button', undefined, 'OK', {name: 'ok'});
// Show dialog and return the selected file type
if (dlg.show() === 1) {
return fileTypeDropdown.selection.text;
} else {
return 'JPEG'; // Default to JPEG if canceled
}
}
// Get output folder from the user
var outputFolder = Folder.selectDialog("Select folder to save layers");
if (outputFolder) {
var fileType = selectFileType();
// Create the save options
var saveOptions;
if (fileType === 'JPEG') {
saveOptions = new JPEGSaveOptions();
saveOptions.quality = 8; // Maximum quality
} else if (fileType === 'PNG') {
saveOptions = new PNGSaveOptions();
}
// Hide all layers
for (var i = 0; i < doc.artLayers.length; i++) {
doc.artLayers[i].visible = false;
}
// Iterate through all layers
for (var i = 0; i < doc.artLayers.length; i++) {
var layer = doc.artLayers[i];
// Export the layer
exportLayer(layer, outputFolder, saveOptions);
}
alert("Export complete!");
} else {
alert("No output folder selected.");
}
} else {
alert("No document is open.");
}
Save this code as an .jsx
file and load this into Photoshop via the
File > Scripts > Browse...
menu. Then you will be asked to choose a
destination folder and choose a filetype (either jpg
or png
with the default
being jpg
). After this, you just have to wait until the script iterate through
all layers. The resulting files will be named after the layers, so in this case
(layers are created via Load files into stack
), they will have exactly the
same name as the source files.
Coding ExtendScript Photography