This is a very short code snippet illustrating how to convert individual pages of PDF documents to TIFF files, one TIFF file per page. It works only on Mac OS X with PyObjC installed. As a recipe the code is compact and does not check for many error conditions, etc. The more extended version does so and can also be used as a command-line tool.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #!/usr/bin/env python
"""pdf2tiff.py - convert PDF to TIFF on mac OS X.
This needs PyObjC 1.0 or higher. A more extended version will be soon
available on my Starship pages: http://python.net/~gherman/pdf2tiff.html
"""
__version__ = '0.5'
__author__ = 'Dinu C. Gherman'
from os.path import splitext
from objc import YES, NO
from Foundation import NSData
from AppKit import NSImage, NSPDFImageRep, NSApplication
NSApp = NSApplication.sharedApplication()
def pdf2tiff(pdfPath, pageNum=0, resolution=72.0):
"Convert one page of a PDF to TIFF at a specific res. in DPI."
tiffPath = "%s-%d.tiff" % (splitext(pdfPath)[0], pageNum)
pdfData = NSData.dataWithContentsOfFile_(pdfPath)
pdfRep = NSPDFImageRep.imageRepWithData_(pdfData)
pageCount = pdfRep.pageCount()
if pageNum > pageCount-1: return
pdfRep.setCurrentPage_(pageNum)
pdfImage = NSImage.alloc().init()
pdfImage.addRepresentation_(pdfRep)
originalSize = pdfImage.size()
width, height = originalSize
pdfImage.setScalesWhenResized_(YES)
rf = resolution / 72.0
pdfImage.setSize_((width*rf, height*rf))
tiffData = pdfImage.TIFFRepresentation()
tiffData.writeToFile_atomically_(tiffPath, YES)
|
This is probably the shortest possible code converting PDF to TIFF on the Mac OS X platform. Converting PDF files to bitmaps is useful for example when making indices, like I do with my imgindex tool (see http://python.net/~gherman/imgindex.html). Apart from that, this is also a nice demo for using PyObjC which gives you the power of reusing all of the Foundation and AppKit from Python. Unfortunately, the generated TIFFs cannot be read by PIL as it seems, which is something to investigate further. On OS X, though, the TIFF files are just ordinary bitmap files.