Does anyone have an example of using the PyObjC bridge to use the CoreGraphics and Quartz libraries in Snow Leopard.

I want to use the example but am not sure how to get it working. I know I have to import objc but do not know what else is required to get it working.

Would appreciate any assistance.


# step 1: import the required modules
import os, sys
from CoreGraphics import *

if len( sys.argv ) != 2:
print "usage: python pdf_filename"

# step 2: read the pdf file name from the command line arguments
pdf_filename = sys.argv[1]
pdf_name, ext = os.path.splitext( pdf_filename )

# NOTE: on Panther use cs = CGColorSpaceCreateDeviceRGB()
cs = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB )

# step 3: create the input PDF document
provider = CGDataProviderCreateWithFilename( pdf_filename )
pdf = CGPDFDocumentCreateWithProvider( provider )
if pdf is None:
print "Error reading PDF document - check that the supplied filename points to a PDF file"

# page number index is 1-based
for page_number in range( 1, pdf.getNumberOfPages() + 1 ):

page_rect = pdf.getMediaBox( page_number )
page_width = int(page_rect.getWidth())
page_height = int(page_rect.getHeight())

# step 4: create an appropriate bitmap and draw the PDF
bitmap = CGBitmapContextCreateWithColor(
page_width, page_height, cs, (1,1,1,1) )
bitmap.drawPDFDocument( page_rect, pdf, page_number )

# step 5: write out the bitmap to a PNG file (and log each file)
page_filename = "%s_%d.png" % (pdf_name, page_number)
print "Writing image: %s, width x height: (%d x %d)" \
% (page_filename, page_width, page_height)
bitmap.writeToFile( page_filename, kCGImageFormatPNG )