Skip to content

Instantly share code, notes, and snippets.

@ILPlais
Last active August 21, 2025 16:11
Show Gist options
  • Select an option

  • Save ILPlais/617a16e81c245e148572a7c60239ad0b to your computer and use it in GitHub Desktop.

Select an option

Save ILPlais/617a16e81c245e148572a7c60239ad0b to your computer and use it in GitHub Desktop.
OCR of scans with splitting double-page
#!/bin/bash
for pdf in *.pdf;
do
# Display the PDF file to process
echo "Processing file \"$pdf\"…"
# Temporary folder
TmpRep="/tmp/conversion$(date +%Y%m%d%H%M%S)"
mkdir --parents "$TmpRep"
# Extract the PDF in the temporary folder
pdfimages "$pdf" "$TmpRep/${pdf%.pdf}"
for img in "$TmpRep"/*.ppm;
do
# Get image size
Larg=$(identify -format "%w" "$img")
Haut=$(identify -format "%h" "$img")
# Check if the image is in landscape
if [ $Larg -gt $Haut ]
then
# Cut it in two portraits
echo "The image \"$img\" is in landscape. It will be cut in two portraits."
convert "$img" -crop 2x1@ "${img%.*}_%d.ppm"
# Delete the original
rm "$img"
fi
done
# Create the PDF file with OCR from the images
img2pdf "$TmpRep"/*.ppm | ocrmypdf --language $1 --deskew --remove-background --clean-final --tesseract-timeout 240 --optimize 3 - "${pdf%.pdf} - OCR.pdf"
# Delete the temporary folder
rm -rf "$TmpRep"/
echo "File processing \"$pdf\" done."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment