Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
Add Comment
Please, Sign In to add comment