Skip to content

Instantly share code, notes, and snippets.

@Blackjacx
Last active May 20, 2017 14:09
Show Gist options
  • Save Blackjacx/49389b25a51281a2a1b051d12ea69f92 to your computer and use it in GitHub Desktop.
Save Blackjacx/49389b25a51281a2a1b051d12ea69f92 to your computer and use it in GitHub Desktop.
Converts all tifs in the current directory to black and white and merges them into a single pdf
#/bin/bash
EXT="tif"
# use xargs to trim whitespaces
COUNT=`ls -1 *.$EXT 2>/dev/null | wc -l | xargs`
THRESHOLD="50%"
# check if there are files of the specified extension
if [ $COUNT != 0 ]
then
echo "Converting $COUNT $EXT's to black and white and merge them to one PDF..."
# create temp dir
TEMP="$(mktemp -d)"
# convert all found images to black and white and merge them in one pdf
convert *.tif -depth 1 -colorspace Gray -threshold "$THRESHOLD" -type bilevel -colors 2 $TEMP/out_%04d.png && convert $TEMP/*.png combined.pdf
#delete temp dir
rm -rf $TEMP
else
echo "No $EXT's found!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment