Skip to content

Instantly share code, notes, and snippets.

@njh
Created September 23, 2024 18:58
Show Gist options
  • Save njh/32f30b57e80851182c0000c26ba202ee to your computer and use it in GitHub Desktop.
Save njh/32f30b57e80851182c0000c26ba202ee to your computer and use it in GitHub Desktop.
Shell script to convert 4x6 label from PDF to monochrome PNG on Mac OS
#!/bin/sh
#
# Bash script to convert 4x6 label from PDF to a monochrome PNG on Mac OS
#
# I wrote this because I was unhappy with the print quality, printing
# directly to a Zebra GX420d using the system print drivers (CUPS).
# In particular dithering could stop barcodes from being scanned.
#
# By scaling and converting to monochrome first, I found that the print quality
# was much better.
#
# By Nicholas Humfrey <njh.me>
#
# Abort if anything fails
set -e
INFILE="$1"
OUTFILE="${INFILE%.pdf}-mono.png"
TMPFILE="$(mktemp -t monolabel).tiff"
if [ ! -f "$INFILE" ]; then
echo "Input file does not exist: $INFILE"
exit 1
fi
echo "Input File: $INFILE"
echo "Output File: $OUTFILE"
## FIXME: support landscape labels
# Use Mac OS tool to convert from PDF to a temporary TIFF file
# (Scriptable Image Processing System)
sips \
--setProperty format tiff \
--setProperty dpiHeight 203 \
--setProperty dpiWidth 203 \
--resampleWidth 812 \
--resampleHeight 1218 \
--padColor FFFFFF \
--out "$TMPFILE" \
"$INFILE"
# FIXME: detect if 'convert' or 'magick' commands are available
# Convert the TIFF to monochrome PNG
# (can't see how to do this using sips)
magick "$TMPFILE -threshold 50% -monochrome $OUTFILE"
# Delete the temporary file
rm -f "$TMPFILE"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment