Skip to content

Instantly share code, notes, and snippets.

@denblackstache
Last active May 1, 2025 09:00
Show Gist options
  • Save denblackstache/5b18377af9a23e81e32ffcd01643bfce to your computer and use it in GitHub Desktop.
Save denblackstache/5b18377af9a23e81e32ffcd01643bfce to your computer and use it in GitHub Desktop.
Simulate hard copy printing and scanning
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 input.pdf"
exit 1
fi
INPUT_PDF="$1"
OUTPUT_PDF="scanned_${INPUT_PDF%.pdf}.pdf"
mkdir -p temp_images
magick -density 150 "$INPUT_PDF" -quality 90 temp_images/page-%03d.jpg
if [ ! -f temp_images/page-000.jpg ]; then
echo "Error: Failed to extract images from PDF. Exiting."
exit 1
fi
for img in temp_images/page-*.jpg; do
OUTPUT_IMG="temp_images/processed-$(basename "$img")"
magick "$img" -blur 0x0.5 +noise Gaussian -level 15%,85% \
-brightness-contrast -2x5 -rotate 0.25 "$OUTPUT_IMG"
if [ ! -f "$OUTPUT_IMG" ]; then
echo "Error processing $img. Skipping."
fi
done
if ls temp_images/processed-*.jpg 1> /dev/null 2>&1; then
magick temp_images/processed-*.jpg -quality 90 "$OUTPUT_PDF"
echo "Processed file saved as: $OUTPUT_PDF"
else
echo "Error: No processed images found. Exiting."
exit 1
fi
rm -r temp_images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment