How to process each page of a multi-page PDF in place with imagemagick?

Viewed 1012

I have a multi-page PDF with photographed book pages. I want to remove gradients from every page to prepare for optical character recognition.

This command works fine on a PNG of a single page:

convert page.png \( +clone -blur 0x64 \) -compose minus -composite -channel RGB -negate page_deblurred.png

However, as soon as I try this on a multi-page PDF by using this command...

convert full.pdf \( +clone -blur 0x64 \) -compose minus -composite -channel RGB -negate full_deblurred.pdf

...I get a single-page PDF with inversed colors overlaid with text from several pages.

How do I tell imagemagick to process every page like it does with the PNG and return a multi-page PDF to me?

3 Answers

It seems unlikely you'd want to pass a PDF to OCR, since Tesseract et al prefer PNG or NetPBM PPM files, so you might as well split your big PDF into individual PNG (or other) files:

convert full.pdf page-%03d.png

You can now remove gradients on individual pages, one at a time, and pass to OCR. Or you can use GNU Parallel to do them in parallel - please say if this in an option and I will write it up for you, if so.

As imagemagick does not seem to be capable to do this in one shot, I put together a script based on the suggestion Mark Setchell made in a comment to his answer.

#!/usr/bin/bash

set -e

tmpdir=$(mktemp -d)

echo "Splitting PDF into single pages"
convert -density 288 "$1" "${tmpdir}/page-%03d.png"
for f in "$tmpdir"/page-*.png
do
    echo "Processing ${f##*/}"
    convert "$f" \( +clone -blur 0x64 \) -compose minus -composite -channel RGB -negate "$(printf "%s%s" "$f" "_gradient_removed.png")"
done
pdf_file_name_without_suffix="${1%.pdf}"
echo "Reassembling PDF"
convert "$tmpdir"/*_gradient_removed.png -quality 100 "$pdf_file_name_without_suffix"_gradient_removed.pdf

rm -rf "${tmpdir}"

It works fine with my material. Your mileage may vary.

This should do what you want in ImageMagick in one command line. You have to use -layers composite and separate your pdf and your blur processing by null:. This is the same process as merging animated gifs.

convert -density 288 image.pdf -write mpr:img null: \( mpr:img -blur 0x64 \) -compose minus -layers composite -channel RGB -negate -resize 25% image_deblurred.pdf


Related