Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow PSDraw Module

Pillow PSDraw Module

PSDraw is a module in the Python Pillow library, specifically used to generate drawing files in PostScript format.

PostScript is a page description language and programming language, widely used in desktop publishing and printing.

The PSDraw module allows you to:

Import method:



from PIL import PSDraw


Main Methods of the PSDraw Module

The following are commonly used methods in the PSDraw module and their functional descriptions:

Method Description Parameters Return Value
PSDraw.begin_document() Start a new PostScript document None None
PSDraw.end_document() End the current PostScript document None None
PSDraw.setfont(font, size) Set the text font and size font: font name
size: font size (points) None
PSDraw.text(position, text) Draw text at the specified position position: (x,y) coordinates
text: text to draw None
PSDraw.line(from_pos, to_pos) Draw a straight line from_pos: starting coordinates
to_pos: ending coordinates None
PSDraw.rectangle(box) Draw a rectangle box: (x0,y0,x1,y1) coordinates None
PSDraw.setink(color) Set the drawing color color: color value (RGB tuple or color name) None
PSDraw.setfill(onoff) Set whether to fill shapes onoff: boolean (True/False) None

Basic Usage Example

Create a simple PostScript document

Example

from

PIL

import

PSDraw

Create a PSDraw object

ps

=

PSDraw.

PSDraw

(

)

 

Outputs to standard output by default

Start the document

ps.

begin_document

(

)

Set the font

ps.

setfont

(

"Helvetica"

,

12

)

Draw the text

ps.

text

(

(

100

,

100

)

,

"Hello, PostScript!"

)

Draw a line

ps.

line

(

(

100

,

120

)

,

(

200

,

120

)

)

Draw a rectangle

ps.

rectangle

(

(

100

,

150

,

200

,

200

)

)

End the document

ps.

end_document

(

)

Output to a file

Example

from

PIL

import

PSDraw

Create file output

with

open

(

"output.ps"

,

"wb"

)

as

f:

    ps

=

PSDraw.

PSDraw

(

f

)

   

   

Set the page size (A4)

    ps.

begin_document

(

"A4"

)

   

   

Set the color to red

    ps.

setink

(

"red"

)

   

   

Draw a filled rectangle

    ps.

setfill

(

True

)

    ps.

rectangle

(

(

100

,

100

,

300

,

200

)

)

   

   

End the document

    ps.

end_document

(

)


Advanced features

Coordinate System

PSDraw uses PostScript's default coordinate system, with the origin (0,0) at the bottom-left of the page, the x-axis increasing to the right, and the y-axis increasing upward. The coordinate unit is the point (1 point = 1/72 inch).

Graphics Transformations

Although PSDraw does not directly provide transformation methods, you can implement translation, rotation, and scaling through PostScript commands:

Example

ps.

text

(

(

100

,

100

)

,

"gsave 45 rotate Hello, World! grestore"

)

Custom PostScript Commands

PSDraw allows you to insert raw PostScript commands directly:

Example

ps.

text

(

(

0

,

0

)

,

"% Custom PostScript commands\n/showpage {} def"

)


Notes

  1. The PSDraw module is mainly used to generate PostScript files, not to display graphics
  2. The generated PostScript files can be viewed with Ghostscript or other PostScript interpreters
  3. Some complex graphics operations may require direct use of PostScript commands
  4. For modern applications, consider using more modern vector graphics formats such as SVG

By mastering the PSDraw module, you can easily generate high-quality PostScript documents in Python, suitable for printing and professional publishing needs.