Converting PDF to PNG with PyPoppler
Intro
Converting PDF to PNG is not something that ImageMagick seems to really excel
in: My input was a PDF of LaTeX Beamer slides, my desired output was a sharp
1024x768 PNG file. Whatever I got out was either (ugly and) scaled (when
tuning calls to convert
of ImageMagick directly) or mis-sized (when tuning
pixels-per-inch resolution in calls to GhostScript; ImageMagick printed that
in verbose mode). If you managed to get either working well I'd be interested
to hear how.
Solution
So after Googling around without too much insight I somehow felt like playing around with the Python bindings of Poppler. In the end I actually got a sharp 1024x768 PNG file out. This is what I did:
import poppler import gtk import urllib input_filename = 'some-presentation.pdf' output_filename = 'frontslide-shot.png' width = 1024 height = 768 doc = poppler.document_new_from_file('file://%s' % \ urllib.pathname2url(input_filename), password=None) page = doc.get_page(0) pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, width, height) page.render_to_pixbuf(src_x=0, src_y=0, src_width=width, src_height=height, scale=width/page.get_size()[0], rotation=0, pixbuf=pixbuf) pixbuf.save(output_filename, 'png')