Make Thumbnails of PDF Pages with ImageMagick
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.
I would like to uise this code in some of serverless solution e.g. Azure function. How to return this thumbnail in a request?