Automating Image Manipulation in Emacs Dired Mode

I manage my images using emacs dired, specifically image-dired. I found myself repeatedly marking images in order to paste their filepaths into various shell scripts for further manipulation.

This article presents a convenient way to automate image manipulation tasks using a Bash script and Emacs Dired mode. It presents an approach to creating a Bash script that resizes images, applies padding and aspect ratio, and removes EXIF information. The script is then bound to a C-c C-x key in Emacs Dired mode, allowing users to easily apply these operations to multiple images with a single keystroke. The article also covers making the script executable and monitoring the output of the script using journalctl.

Image manipulation script

#~/bin/image_manipulation.sh
outdir="/tmp/$(date +%Y%m%d_%H%M%S)"  # Create a directory with the current date and time
mkdir $outdir

for arg in "$@"; do
    source="$arg"  # Store the source file path
    filename="${source##*/}"  # Extract the filename from the source file path
    outfile="$outdir/$filename"  # Specify the output file path
    tmpfile="$outdir/tmp_$filename"  # Specify the temporary file path
    logger "Source: $arg, outfile: $outfile, tmpfile: $tmpfile"  # Log the source, output, and temporary file paths

    convert "$source" -resize 1350x1350 "$tmpfile"  # Resize the image
    ~/Downloads/aspectpad -a 1.25 -m al -p white "$tmpfile" "$outfile"  # Apply padding and aspect ratio

    # Remove all EXIF information from the image except the Orientation tag
    exiftool -overwrite_original -all= -tagsfromfile @ -Orientation "$outfile"

    rm $tmpfile  # Remove the temporary file
done

# Save the outdir path in the Emacs killring for easy access
echo "$outdir" > /tmp/kill

exit

Make the script executable

sudo chmod +x ~/bin/image_manipulation.sh

Bind script to key in emacs dired mode

(defun my-dired-send-to-image_manipulation ()
  "Send marked files in Dired to ~/bin/image_manipulation.sh
  assure ~/bin is in $PATH)"
  (interactive)
  (let ((files (dired-get-marked-files)))
    (shell-command (concat "image_manipulation.sh " (mapconcat #'shell-quote-argument files " ")))
    (message "Sent to image_manipulation.sh: %s" files)))
(define-key dired-mode-map (kbd "C-c C-x") 'my-dired-send-to-image_manipulation)

Check script output in log

journalctl -f