grabs2pdf.sh

A script that will crop a series of screenshots according to a manually provided sample crop, and then apply OCR to create a PDF combining the cropped screenshots and the text that was recognized.

Requires:

  • compare, convert, identify, mogrify imagemagick
  • tesseract tesseract-ocr or tesseract (+tesseract-ocr-fra, tesseract-ocr-spa, … or tesseract-data-fra, tesseract-data-spa, … )

The script controls the viewer application by sending virtual key presses. It makes the viewer app go fullscreen at the beginning, then takes a screenshot, and sends the appropriate key press to make the viewer advance by one page, before grabbing the next screenshot.

How to call it (after starting it, you have a few seconds to bring the viewer application window to the front):

grabs2pdf.sh -l language -o outfileWithoutExtension

E.g.:

grabs2pdf.sh -l eng -o ./mydoc

You’ll most likely want to apply this script to the results of viewer2grabs.sh.

To download this script, first hover with your mouse over the listing below and press »Open code in new window«. Copying and pasting the colored and formatted listing below, as-is, won’t work.
#!/bin/bash

language=eng # Remember to install required tesseract language package: eng;spa;fra;deu;...
outfile=./grabbed

while getopts ":l:o:" opt; do
  case $opt in
    l) language="$OPTARG"
    ;;
    o) outfile="$OPTARG"
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    echo "Usage:" >&2
    echo "$(basename $0) -l language -o outfileWithoutExtension"
    echo
    echo "Examples:"
    echo "$(basename $0)"
    echo "$(basename $0) -l $language"
    echo "$(basename $0) -l $language -o $outfile"
    exit 1
    ;;
  esac
done
shift $((OPTIND-1))

echo "Executing: $(basename $0) -l $language -o $outfile"


GRABBED_SCREENS_DIR=./screens
CROPPED_DIR=./cropped


# Find cropped part in whole

first_grab="$GRABBED_SCREENS_DIR"/grabbed-0001.png
cropping_aid="$GRABBED_SCREENS_DIR"/please_crop_me.png

GRABBED_WIDTH=$(identify -format '%w' "$first_grab")
GRABBED_HEIGHT=$(identify -format '%h' "$first_grab")

CROPPED_WIDTH=$(identify -format '%w' "$cropping_aid")
CROPPED_HEIGHT=$(identify -format '%h' "$cropping_aid")

convert "$first_grab" -resize 25% "$GRABBED_SCREENS_DIR"/tmpwinsmall.png
convert "$cropping_aid" -resize 25% "$GRABBED_SCREENS_DIR"/tmpcroppedsmall.png

echo Detecting crop position and dimensions from sample crop you created...
crop_pos=$(compare -metric Fuzz -subimage-search "$GRABBED_SCREENS_DIR"/tmpwinsmall.png "$GRABBED_SCREENS_DIR"/tmpcroppedsmall.png null: 2>&1 | sed s/.*@\ //g)
crop_pos_x=$(( 4*${crop_pos%,*} ))
crop_pos_y=$(( 4*${crop_pos#*,} ))

echo Now cropping all grabbed pages, according to your sample...
mkdir -p "$CROPPED_DIR"
rm "$CROPPED_DIR"/grabbed-*.png

mogrify -format png -density 300 -path "$CROPPED_DIR" -crop ${CROPPED_WIDTH}x${CROPPED_HEIGHT}+${crop_pos_x}+${crop_pos_y} +repage "$GRABBED_SCREENS_DIR"/grabbed-*.png

find "$CROPPED_DIR" -iname 'grabbed-*.png' -type f -printf "$CROPPED_DIR/%f\n" | sort > "$CROPPED_DIR"/pagelist.txt
echo Now creating $(wc -l "$CROPPED_DIR"/pagelist.txt)-page PDF via OCR...
tesseract -l $language --psm 1 --dpi 300 "$CROPPED_DIR"/pagelist.txt "$outfile" pdf

echo
echo Finished.
echo See "$outfile".pdf

Image Credits:
Papirus icon for Terminal (modified) | GNU General Public License, version 3

Licensing:
This content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
For your attributions to us please use the word »tuxwise«, and the link https://tuxwise.net.