When handling PDF with ImageMagick, GS must be installed.
http://superuser.com/questions/819277/cant-convert-pdf-into-image-because-of-no-images-defined-error
If you are using brew on OSX, you can use brew install gs
.
If you don't set the dpi with the density
option, the image will be very grainy.
Set according to the original PDF settings and the size of the image you want to output.
Referenced site http://icepotato.cocolog-nifty.com/blog/2013/06/imagemagickpdfw.html
With Python Pillow, PDF seems to be writing only and not reading.
The sampling resolution is 300dpi, the margins are trimmed, and the PNG image is exported to the same folder.
It should work if you have ImageMagick installed.
# coding: utf-8
import os
import os.path
import fnmatch
import subprocess
def execute(root_path):
for dirpath, _, filenames in os.walk(root_path):
for filename in filenames:
if fnmatch.fnmatch(filename, u"*.pdf"):
org_path = os.path.join(dirpath, filename)
png_path = org_path.replace(".pdf", ".png ")
print "convert {0} to {1}".format(org_path, png_path)
if subprocess.call(["convert", "-density", "300", "-trim", org_path, png_path]) != 0:
print "failed: {0}".format(org_path)
if __name__ == '__main__':
root_path = raw_input("target folder path> ")
execute(root_path)
Recommended Posts