Using a Bash Script to Extract Layers from PSD Files with ImageMagick

2 min read .

In this tutorial, we’ll discuss how to use a Bash script to extract layers from PSD (Photoshop Document) files into image formats with the help of ImageMagick. This script will allow you to automatically process PSD files and save the image layers as PNG files.

Prerequisites

Before proceeding, ensure you have the following prerequisites:

  1. ImageMagick: Make sure ImageMagick is installed on your system. If not, you can install it using:

    sudo apt install imagemagick
  2. PSD File: You should have a PSD file that you want to process.

Bash Script for Extracting Image Layers

Here is a Bash script that processes PSD files and extracts the image layers into PNG format:

#!/bin/bash

# Check if a PSD file is provided as an argument
if [ -z "$1" ]; then
  echo "Usage: $0 <input.psd>"
  exit 1
fi

# Check if ImageMagick is installed
if ! command -v convert &> /dev/null; then
  echo "ImageMagick is not installed. Please install it using 'sudo apt install imagemagick'."
  exit 1
fi

# Process each PSD file provided as an argument
for input_psd in "$@"; do
  # Extract filename from path
  filename=$(basename -- "$input_psd")
  # Extract directory from path
  directory=$(dirname -- "$input_psd")
  # Remove file extension (.psd) from filename
  filename_no_ext="${filename%.*}"

  # Create output directory if it does not exist
  output_directory="image/$filename_no_ext"
  mkdir -p "$output_directory"

  # Extract all image layers from PSD file to output directory
  convert "$input_psd" "$output_directory/image.png"

  echo "All image layers from '$input_psd' have been extracted to '$output_directory'."
done

Script Explanation

  1. Checking Arguments:

    • if [ -z "$1" ]; then ... checks if a PSD file argument is provided. If not, the script displays a usage message and exits.
  2. Checking ImageMagick:

    • if ! command -v convert &> /dev/null; then ... checks if ImageMagick is installed. If not, the script displays a message and exits.
  3. Processing Each PSD File:

    • for input_psd in "$@"; do ... iterates through each PSD file provided as an argument.
  4. Creating Output Directory:

    • output_directory="image/$filename_no_ext" specifies the output directory for saving extracted images. mkdir -p "$output_directory" creates the directory if it does not exist.
  5. Extracting Image Layers:

    • convert "$input_psd" "$output_directory/image.png" uses the convert command from ImageMagick to extract image layers from the PSD file and save them in PNG format in the output directory.
  6. Displaying Message:

    • echo "All image layers from '$input_psd' have been extracted to '$output_directory'." displays a confirmation message after processing is complete.

Conclusion

With this Bash script, you can easily extract image layers from PSD files and save them as PNG files. This script is very useful for batch processing PSD files, especially when you need to process many files efficiently. Ensure you have ImageMagick installed and that the script has execution permissions before running it.

Tags:
Linux

See Also

chevron-up