Skip to content

Instantly share code, notes, and snippets.

@mesheets
Last active November 10, 2024 03:07
Show Gist options
  • Select an option

  • Save mesheets/4db0e1db9608ba199947b66f1d8cd2a8 to your computer and use it in GitHub Desktop.

Select an option

Save mesheets/4db0e1db9608ba199947b66f1d8cd2a8 to your computer and use it in GitHub Desktop.
Extracts images embedded in a PDF file to a TIFF image file
#!/bin/sh
# Extracts images embedded in a PDF file (such PDFs created from document scanning) to a TIFF image file.
# SUSE Bash - https://documentation.suse.com/sles/12-SP4/html/SLES-all/cha-adm-shell.html
# - General shell scripting - https://www.shellscript.sh/
# pdfimages manual - https://www.xpdfreader.com/pdfimages-man.html
# - NOTE: use the "-list" option to retreive details (including resolution) for each image
# - c.f https://stackoverflow.com/a/11699848
# - see also https://graphicdesign.stackexchange.com/q/5670
# tiffcp manual - https://libtiff.gitlab.io/libtiff/tools/tiffcp.html
# For openSUSE Tumbleweed, package dependencies include the following:
# - poppler-tools
# - tiff
# As the tiff2pdf command has been removed from the tiff package,
# consider "convert" in the ImageMagick package as an alternative.
# - https://www.reddit.com/r/voidlinux/comments/197fy0n/?rdt=58586
if [ -z "$1" ] || [ ! -e "$1" ] ; then
echo "Usage: `basename $0` <Input PDF File> [Output TIFF File]"
else
INPUT_FILE="$1"
OUTPUT_FILE=$([ -z "$2" ] && echo "${INPUT_FILE%.*}.tif" || echo "$2")
TIFF_TEMP_DIR=`mktemp -d tiff-page-cache_XXXXXXX --tmpdir`
TIFF_TEMP_FILE=$TIFF_TEMP_DIR/rotated-split.tif
pdfimages -tiff "$INPUT_FILE" "$TIFF_TEMP_DIR/page"
tiffcp -c zip $TIFF_TEMP_DIR/*.tif "$OUTPUT_FILE"
# Optionally handle full-spread book scans
if ! true ; then
tiffcrop -R 90 -S 2:1 "$OUTPUT_FILE" "$TIFF_TEMP_FILE"
mv -f "$TIFF_TEMP_FILE" "$OUTPUT_FILE"
fi
trap "rm -d -r '$TIFF_TEMP_DIR'" EXIT
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment