Skip to content

Instantly share code, notes, and snippets.

@jessevanherk
Created July 20, 2014 05:28
Show Gist options
  • Save jessevanherk/c5b65d964a20ab047951 to your computer and use it in GitHub Desktop.
Save jessevanherk/c5b65d964a20ab047951 to your computer and use it in GitHub Desktop.
#!/bin/bash
# flattenpsd - A simple script to read a PSD and save each layer as a PNG.
#
INPUT_FILE=$1
OUTPUT_DIR=$2
if [ "$2" == "" ]; then
echo "Usage: $0 <input_psd> <output_dir>"
exit 1
fi
if [ ! -r $INPUT_FILE ]; then
echo "unable to open input file '$INPUT_FILE'"
exit 2
fi
if [ ! -d $OUTPUT_DIR ]; then
echo "output dir doesn't exist or is not a directory"
exit 2
fi
# find out how many layers are in the PSD
num_layers=`convert $INPUT_FILE -format "%[scenes]" info: | head -n 1`
# for some reason krita's PSDs have an extra layer that breaks things. ignore it.
let num_layers=num_layers-1
echo "exporting $num_layers layers."
# loop through each layer
for x in `seq 1 $num_layers`; do
# get the label name and use that in your convert
label=`convert $INPUT_FILE[$x] -verbose info: | grep "label:" | sed -e "s/label://"|sed "s/^ *//"`
echo "exporting layer '$label'"
# convert to a png.
# use 'trim' to auto-crop the transparent parts as well. IM is awesome!
convert $INPUT_FILE[$x] -trim "$OUTPUT_DIR/${label}.png"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment