Skip to content

Instantly share code, notes, and snippets.

@KorigamiK
Created November 1, 2024 17:36
Show Gist options
  • Save KorigamiK/a43f519d4ba92ff5f1acc47c237e8d92 to your computer and use it in GitHub Desktop.
Save KorigamiK/a43f519d4ba92ff5f1acc47c237e8d92 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if required tools are installed
command -v pdfimages >/dev/null 2>&1 || { echo "pdfimages is required but not installed. Install poppler-utils."; exit 1; }
command -v img2pdf >/dev/null 2>&1 || { echo "img2pdf is required but not installed. Install img2pdf."; exit 1; }
# Create output directory if it doesn't exist
mkdir -p output_pdfs
mkdir -p temp_images
# Process each PDF file in the current directory
for pdf in *.pdf; do
if [ -f "$pdf" ]; then
echo "Processing: $pdf"
# Create a temporary directory for this PDF's images
temp_dir="temp_images/${pdf%.*}"
mkdir -p "$temp_dir"
# Extract images from PDF
pdfimages -all "$pdf" "$temp_dir/image"
# Check if any images were extracted
if ls "$temp_dir"/image* 1> /dev/null 2>&1; then
# Create new PDF from extracted images
img2pdf "$temp_dir"/image* -o "output_pdfs/${pdf%.*}_images.pdf"
echo "Created: output_pdfs/${pdf%.*}_images.pdf"
else
echo "No images found in $pdf"
fi
# Clean up temporary files
rm -rf "$temp_dir"
fi
done
# Clean up main temporary directory
rm -rf temp_images
echo "Processing complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment