< Previous PageNext Page > Hide TOC

Python Bindings for Quartz 2D

Python is an interpreted, object-oriented scripting language that promotes rapid development. Starting with Mac OS X v10.3, Quartz supports the Python scripting language. Anyone who can script in Python has easy access to the powerful Quartz 2D rendering and PDF document model.

The Quartz 2D Python API is a simple wrapper of the Quartz 2D API; most Quartz 2D functions are wrapped. For example, in Quartz 2D the code to draw an image is:

CGContextDrawImage (ctx, rect, image);

Using Python, the code is:

ctx.drawImage (rect, image);

You can write simple Python scripts that perform graphics operations, including batch processing. In many cases, Python scripts are more practical to use than writing a full-blown application that uses the Quartz 2D API directly.

The Quartz 2D–Python API is described in the API summary in the following directory, available after you install the Xcode Tools CD:

/Developer/Examples/Quartz/Python

Note: The Python routine CGFloatArray is not described in the API summary. This routine creates an array of floating-point values. You can use this routine to create an array that is suitable for passing to the Python setLineDash function. For example:

dashes = CGFloatArray(2)
dashes[0] = 10.
dashes[1] = 5.
c.setLineDash(0, dashes, 2)

The Python directory also contains the Python scripts listed in Table 16-1, many of which are less than a page long. Apple might add scripts to this library, so it’s best to check the directory for the most up-to-date list.

Table 16-1  Python scripts available in the Examples folder

Script name

Comments

bitmap.py

Creates an RGB bitmap context with a transparent background and draws it into a PNG file.

circle.py

Draws a circle into a PDF file.

cmyk-bitmap.py

Creates a CYMK bitmap context with an opaque white background.

contactsheet.py

Creates a contact sheet from a directory of JPEG documents.

cover.py

Adds an automatically generated cover page to a PDF document.

doc2pdf.py

Converts any of several document types (text, RTF, HTML, and so forth) to a PDF document.

filter-pdf.py

Applies a ColorSync filter to each page of a PDF document.

image.py

Imports an image and creates an output document to draw the image into.

pict2pdf.py

Creates a PDF file from a PICT file.

ps-to-cmyk.py

Converts files from EPS to CMYK TIFF files.

watermark.py

Adds a watermark to each page of a PDF document.

wedge.py

Uses Python to replicate the PostScript cookbook wedge example.



< Previous PageNext Page > Hide TOC


© 2001, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-12-11)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.