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.
Script name | Comments |
---|---|
| Creates an RGB bitmap context with a transparent background and draws it into a PNG file. |
| Draws a circle into a PDF file. |
| Creates a CYMK bitmap context with an opaque white background. |
| Creates a contact sheet from a directory of JPEG documents. |
| Adds an automatically generated cover page to a PDF document. |
| Converts any of several document types (text, RTF, HTML, and so forth) to a PDF document. |
| Applies a ColorSync filter to each page of a PDF document. |
| Imports an image and creates an output document to draw the image into. |
| Creates a PDF file from a PICT file. |
| Converts files from EPS to CMYK TIFF files. |
| Adds a watermark to each page of a PDF document. |
| Uses Python to replicate the PostScript cookbook wedge example. |
© 2001, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-12-11)