mik01aj
mik01aj

Reputation: 12382

How to quickly generate PNG thumbnails of all pages of a PDF?

I use a following code to do this:

for i in `seq 1 $numPages`; do
    convert "$INPUTPDF"[$((i-1))] thumb_$i.png
done;

It's kinda slow, and I think it's because it starts a new process every time.

How to do this faster?

Thanks very much in advance!

Upvotes: 3

Views: 1300

Answers (1)

sarnold
sarnold

Reputation: 104050

The following was shamelessly stolen:

mikal@deathstar:~/foo$ convert foo.pdf pages-%03d.png
mikal@deathstar:~/foo$ ls pages*
pages-000.png  pages-001.png  pages-002.png  pages-003.png  pages-004.png
mikal@deathstar:~/foo$

This will parse your pdf only once. Woot.

Depending upon how long this process takes, it might make sense to run several of these simultaneously. If you've got a multi-core or SMP machine, you might see benefit to running two, three, or maybe even four convert(1) processes at once. The easy way would be to place commands like this into two or more shell scripts, and then run the shell scripts in parallel. You could generate these scripts like this:

for f in *.pdf ; do echo "convert $f `basename $f .pdf`-%03d.png" >> /tmp/runme ; done
wc -l /tmp/runme
split -l [number of lines/2] /tmp/runme
sh /tmp/xaa & sh /tmp/xab &

This is pretty hacked-together though. If you're going to be doing this often, I'd suggest writing a Makefile for make(1) or your own little process-manager tool to take advantage of multiple CPUs. Heck, maybe something like that already exists. :)

Upvotes: 3

Related Questions