Created
March 6, 2023 12:59
-
-
Save ShenZhouHong/67c5440b157634c132d9ae625efd010a to your computer and use it in GitHub Desktop.
Bash script that extracts image files from PDFs in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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