Make Thumbnails of PDF Pages with ImageMagick

All my talk slides are PDF – I use rst2pdf to transform text-based ReStructuredText content into presentation slides. With all these PDFs hanging around, it can be very handy to have them as thumbnails. I use the images both in the printable speaker notes that I produce (and I should blog that too now I’ve mentioned it), and to share on twitter – especially the resources slide that everyone photographs! My image file is much more readable than your cameraphone picture in terrible lighting :) So here’s my script for thumbnails in case you want to do the same; most presentation tools will export to PDF if you’re not already working in that format.

ImageMagick

I’m a huge ImageMagick fan and it’s one of the open source tools that I rely on hugely. There are probably graphical frontends that use it too, but I’m a command-line sort of a girl and I also automatically regenerate the thumbnails when I update my slides.

The imagemagick command to work with images is convert so to get the first slide as an image, I can do something like:

convert presentation.pdf[0] slide1.png

This outputs an image, which is nice. But I want all the slides as thumbnails so I need to go a step further.

Building ImageMagick Commands with Python

There is probably a neater way to do this (answers in the comments please!) but I already use PyPDF2 for a bunch of PDF manipulation such as grabbing out my speaker notes, so I stuck with that approach and simply asked python to generate and then run a convert command for each of the slides it finds in my PDF.

Here’s the code; it lives in a file called thumbnails.py:

import sys
import PyPDF2, traceback
from subprocess import call

try :
    src = sys.argv[1]
except :
    src = r'/path/to/my/file.pdf'

input1 = PyPDF2.PdfFileReader(open(src, "rb"))
nPages = input1.getNumPages()

for i in range(nPages) :
    # generate the thumbnail
    cmd = 'convert ' + src + '[' + str(i) + '] thumbnails/thumbnail' + str(i) + '.png'
    call(cmd, shell=True)

Run it with python3 thumbnails.py presentation.pdf and it will write a series of thumbnails to the thumbnails/ directory. Which you should create before you run the script, I just didn’t mention it until now, sorry.

Hope it’s useful!


Also published on Medium.

One thought on “Make Thumbnails of PDF Pages with ImageMagick

  1. I would like to uise this code in some of serverless solution e.g. Azure function. How to return this thumbnail in a request?

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.