Skip to content

Instantly share code, notes, and snippets.

@ShenZhouHong
Created March 6, 2023 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShenZhouHong/67c5440b157634c132d9ae625efd010a to your computer and use it in GitHub Desktop.
Save ShenZhouHong/67c5440b157634c132d9ae625efd010a to your computer and use it in GitHub Desktop.
Bash script that extracts image files from PDFs in a directory
#!/bin/bash
# set the exit status to non-zero (failure) if any commands fail
set -e
# set the exit status to non-zero (failure) if any variable is referenced before being set
set -u
# set the shell to be more strict
set -o pipefail
function show_usage {
echo "Usage: $0 [-h|--help] directory"
echo "Extracts images from all PDF files in the specified directory."
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit."
}
# check if the directory argument was provided
if [ "$#" -ne 1 ]; then
show_usage
exit 1
fi
# check if the -h or --help option was provided
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_usage
exit 0
fi
# set the directory to the argument provided
dir="$1"
# make sure the directory exists
if [ ! -d "$dir" ]; then
echo "Error: $dir is not a directory."
exit 1
fi
# make sure the directory contains PDF files
if [ $(find "$dir" -name '*.pdf' | wc -l) -eq 0 ]; then
echo "Error: $dir does not contain any PDF files."
exit 1
fi
# loop through all PDF files in the directory
i=0
total=$(find "$dir" -name '*.pdf' | wc -l)
for file in "$dir"/*.pdf; do
i=$((i + 1))
echo "Processing $file $i/$total..."
pdfimages -j -all "$file" "${file%.pdf}"
done
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment