Convert PSD files to PNG with ImageMagick in a simple bash script

Keeping original PSD files was quite good practice over the years but mostly other formats are needed down the road. Because opening Photoshop to export a file takes quite some time and isn’t efficient I wrote a small script to replace this task:

#!/usr/bin/env bash

if [[ "" == "$@" ]]; then
    echo "usage: $0 <file(s) to convert>"
    exit 1
fi

for i in "$@"; do
    directory=$(realpath $(dirname "$i"))
    file=$(basename "$i")
    name=${file%.*}
    extension=$(echo ${file##*.} | tr '[:upper:]' '[:lower:]')

    if [[ "$extension" == "psd" ]]; then
        target="$directory/$name.png"
        if [[ -e "$target" ]]; then
            echo "file already exists: '$target'"
        else
            echo "converting '$i'"
            convert "$i" -background none -flatten "$target" || exit 1
        fi
    fi
done

This takes the file(s) to convert as argument(s). You can also just go with wildcards like *.psd to convert all psd files in a folder. Already converted files will be skipped.

You need ImageMagic installed on your machine to have access to the convert command.

Tested with ImageMagic 7.1.1-27 on MacOS Sonoma 14.3.1.