2
# The Python Imaging Library
5
# WCK-style drawing interface operations
8
# 2003-12-07 fl created
9
# 2005-05-15 fl updated; added to PIL as ImageDraw2
10
# 2005-05-15 fl added text support
11
# 2005-05-20 fl added arc/chord/pieslice support
13
# Copyright (c) 2003-2005 by Secret Labs AB
14
# Copyright (c) 2003-2005 by Fredrik Lundh
16
# See the README file for information on usage and redistribution.
19
import Image, ImageColor, ImageDraw, ImageFont, ImagePath
22
def __init__(self, color, width=1, opacity=255):
23
self.color = ImageColor.getrgb(color)
27
def __init__(self, color, opacity=255):
28
self.color = ImageColor.getrgb(color)
31
def __init__(self, color, file, size=12):
32
# FIXME: add support for bitmap fonts
33
self.color = ImageColor.getrgb(color)
34
self.font = ImageFont.truetype(file, size)
38
def __init__(self, image, size=None, color=None):
39
if not hasattr(image, "im"):
40
image = Image.new(image, size, color)
41
self.draw = ImageDraw.Draw(image)
48
def render(self, op, xy, pen, brush=None):
49
# handle color arguments
50
outline = fill = None; width = 1
51
if isinstance(pen, Pen):
54
elif isinstance(brush, Pen):
57
if isinstance(brush, Brush):
59
elif isinstance(pen, Brush):
61
# handle transformation
63
xy = ImagePath.Path(xy)
64
xy.transform(self.transform)
67
self.draw.line(xy, fill=outline, width=width)
69
getattr(self.draw, op)(xy, fill=fill, outline=outline)
71
def settransform(self, (xoffset, yoffset)):
72
self.transform = (1, 0, xoffset, 0, 1, yoffset)
74
def arc(self, xy, start, end, *options):
75
self.render("arc", xy, start, end, *options)
77
def chord(self, xy, start, end, *options):
78
self.render("chord", xy, start, end, *options)
80
def ellipse(self, xy, *options):
81
self.render("ellipse", xy, *options)
83
def line(self, xy, *options):
84
self.render("line", xy, *options)
86
def pieslice(self, xy, start, end, *options):
87
self.render("pieslice", xy, start, end, *options)
89
def polygon(self, xy, *options):
90
self.render("polygon", xy, *options)
92
def rectangle(self, xy, *options):
93
self.render("rectangle", xy, *options)
95
def symbol(self, xy, symbol, *options):
96
raise NotImplementedError("not in this version")
98
def text(self, xy, text, font):
100
xy = ImagePath.Path(xy)
101
xy.transform(self.transform)
102
self.draw.text(xy, text, font=font.font, fill=font.color)
104
def textsize(self, text, font):
105
return self.draw.textsize(text, font=font.font)