Skip to content

Instantly share code, notes, and snippets.

@nottrobin
Last active January 23, 2016 14:25
Show Gist options
  • Select an option

  • Save nottrobin/fc3e82afbb28f673ecc8 to your computer and use it in GitHub Desktop.

Select an option

Save nottrobin/fc3e82afbb28f673ecc8 to your computer and use it in GitHub Desktop.
An imagemagick script to convert a PDF to a greyscaled & inverted version (for a unix-based system)
#!/usr/bin/env bash
##
# Usage:
# ./grey-invert.sh my-doc.pdf
#
# This will:
# - Convert to images and store in a /tmp directory
# - Greyscale images
# - Invert images
# - Rename images to their proper order
# - Convert images back to a PDF named "{filename}-grey-invert.pdf"
#
# The only dependency is imagemagick
##
file=$1
dir=$(mktemp -d)
convert -density 150 ${file} -quality 90 ${dir}/${file}.png
mogrify -colorspace Gray ${dir}/*.png
mogrify -negate ${dir}/*.png
rename 's/-(\d+)/sprintf("-%05d", $1)/e' ${dir}/*.png
convert ${dir}/*.png ${file}-grey-invert.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment