Skip to content

Instantly share code, notes, and snippets.

@geographika
Created January 18, 2011 18:17
Show Gist options
  • Save geographika/784880 to your computer and use it in GitHub Desktop.
Save geographika/784880 to your computer and use it in GitHub Desktop.
A simple script to generate thumbnail previews of PDFs. Requires ImageMagick and Ghostscript
import subprocess
import shlex, os, glob
#ensure ImageMagick and Ghostscript are installed
#tested on Python 2.7 / Windows XP
#this folder contains the convert.exe
image_magick_path = "C:\\Program Files\\ImageMagick-6.6.7-Q16\\"
os.chdir(image_magick_path)
#the folder containing the PDFs
pdf_files_path = "D:\\Temp\\pdfs\\"
pdf_files = glob.glob('%s*.pdf' % pdf_files_path)
#the folder to store the generated images
output_path = "D:\\Temp\\thumbnails\\"
#use the startupinfo to hide the Windows Shell
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW #subprocess.STARTF_USESHOWWINDOW
for pdf in pdf_files:
filename = os.path.basename(pdf)
#create a new output filename
output_file = output_path + os.path.splitext(filename)[0] + ".png"
#this is the windows shell command
command_line = """convert.exe -quality {quality} -border 3x3
-bordercolor #000000 "{filename}"[0] -thumbnail 100x120 "{pngfile}" """.format(quality=100,
filename = pdf, pngfile = output_file)
args = shlex.split(command_line)
#run the command
proc = subprocess.Popen(args, stderr=subprocess.PIPE,
stdout=subprocess.PIPE, stdin=subprocess.PIPE, startupinfo=startupinfo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment