New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

Start a Discussion

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Snow Lepoard Python CoreGraphics Example

Post Reply New Thread Subscribe

Thread Tools
Mike B

Member Since: Dec 10, 2009
Posts: 1
Mike B is on a distinguished road

Mike B is offline
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 )
QUOTE Thanks

Post Reply New Thread Subscribe

« Unzipping in Mac C++ | Web service with Apache »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Starter
Last Post
can I update this to snow lepoard? bgardner15 OS X - Operating System 2 12-07-2009 12:21 PM
Snow lepoard running crazy slow BostonMarley OS X - Operating System 46 10-18-2009 01:25 PM
Snow Lepoard drawbacks elwitz OS X - Operating System 4 08-27-2009 05:34 PM
Client-side cookies on Python in Mac OSX Hikaru79 OS X - Development and Darwin 0 07-12-2007 10:25 PM

All times are GMT -4. The time now is 05:36 PM.

Powered by vBulletin
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.

Welcome to

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!

(4 digit year)

Already a member?