My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#! /usr/bin/env python

#
# Copyright (c) 2011 Brian House
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See <http://www.gnu.org/licenses/gpl.html> for details.
#

"""

Description
===========

A simple drawing interface for Python that wraps PIL and aggdraw.

For slightly more information, see http://blog.brianhouse.net/post/6836163797


Requirements
============

Python 2.6+
PIL http://www.pythonware.com/products/pil/
aggdraw http://effbot.org/zone/aggdraw-index.htm (for 64-bit systems (eg OSX), use https://bitbucket.org/2degrees/aggdraw-64bits/src)


Using
=====

See the __main__ function below for a quick example.

Note on colors:
Colors are specified as a tuple of R, G, B, A values in the default mode and H, S, V, A if hsv was indicated in the class constructor. Alpha is optional.
If ints are used, 0-255 is assumed, whereas floats imply a 0-1 scale. Values can be mixed.
'stroke' refers to the lines/borders of a shape, with the supplied 'thickness'. To draw shapes without borders, set thickness to 0 and specify a fill.

Also, note that, unlike Processing or Canvas, this is not a state machine -- values must be specified with every command.


Todo
====

Polygons / polylines
Rotated shapes

Mouse events:

cv.NamedWindow("radar")
def on_mouse(event, x, y, flags, param):
if event == cv.CV_EVENT_LBUTTONUP:
print("click: %s,%s" % (x, y))
if event == cv.CV_EVENT_MOUSEMOVE:
print("move: %s,%s" % (x, y))
cv.SetMouseCallback("radar", on_mouse)

Keyboard events:

key = cv.WaitKey(1)


"""

import aggdraw, colorsys, math, time
from PIL import Image, ImageFilter, ImageOps, ImageDraw, ImageFont
from log import log

class Context(object):
"""The drawing context"""

def __init__(self, width=800, height=600, background=None, hsv=False, flip=False, relative=False, margin=0):
""" Create a drawing context of a given size and background color. 'hsv' specifies if we should use hsv color values instead of the default rgb.
'flip' inverts the vertical axis.
'relative' indicates that all methods will take x, y, width, and height values as floats from 0-1 scaled relatively across the canvas.
'margin' adds margin pixels to all sides that are outside of the coordinate system.
These flags may be useful for looking at scaled data without having to convert to pixel positions.
"""
self._width = float(width)
self._height = float(height)
self._hsv = hsv
self.background = background
if not background:
self.background = (0.0, 0.0, 1.0) if self._hsv else (1.0, 1.0, 1.0)
self._ctx = None
self.mode = "RGBA" if len(self.background) > 3 else "RGB"
self._image = Image.new(self.mode, (width, height), self._handle_color(self.background))
self._verify_context()
self._valid = True
self._horiz = (lambda x: x + margin) if not relative else (lambda x: (x * (self.width - (2.0 * margin))) + margin)
self._flip = flip
self._perform_flip = (lambda y: float(self.height) - y) if flip else (lambda y: y)
self._vert = (lambda y: self._perform_flip(y + margin)) if not relative else (lambda y: self._perform_flip((y * (self.height - (2.0 * margin))) + margin))

def _verify_context(self):
if not self._ctx:
self._ctx = aggdraw.Draw(self._image)

def _render(self):
if not self._valid and self._ctx:
self._ctx.flush()
self._valid = True

def _handle_color(self, args):
if type(args) == float or type(args) == int:
args = [args] * 3
color = [0.0, 0.0, 0.0, 1.0]
for i, arg in enumerate(args):
color[i] = arg
if type(arg) == int:
color[i] /= 255.0
if self._hsv:
color[0], color[1], color[2] = colorsys.hsv_to_rgb(*color[:3])
return int(color[0] * 255.0), int(color[1] * 255.0), int(color[2] * 255.0), int(color[3] * 255.0)

"""Public Methods"""

@property
def width(self):
"""Return the width of the drawing context."""
return self._width

@property
def height(self):
"""Return the height of the drawing context."""
return self._height

@property
def image(self):
"""Return the drawing context as a PIL image."""
if not self._valid:
self._render()
return self._image

def line(self, x1, y1=None, x2=None, y2=None, stroke=(0.0, 0.0, 0.0, 1.0), thickness=1.0):
"""Draw a line from x1,y1 to x2,y2, or between all points (as x,y pairs) in a list."""
self._verify_context()
stroke = self._handle_color(stroke)
pen = aggdraw.Pen(stroke[:3], thickness, stroke[3])
if type(x1) == tuple or type(x1) == list:
points = []
for point in x1:
points.extend((self._horiz(point[0]), self._vert(point[1])))
self._ctx.line(points, pen)
else:
self._ctx.line((self._horiz(x1), self._vert(y1), self._horiz(x2), self._vert(y2)), pen)
self._valid = False

def curve(self, x1, y1, xc, yc, x2, y2, stroke=(0.0, 0.0, 0.0, 1.0), thickness=1.0, steps=100):
"""Draw a curve from x1,y1 to x2,y2 with control point xc, yc. TODO: arbitrary number of control points, dynamic steps selection."""
self._verify_context()
stroke = self._handle_color(stroke)
pen = aggdraw.Pen(stroke[:3], thickness, stroke[3])
pt0 = self._horiz(x1), self._vert(y1)
pt1 = self._horiz(xc), self._vert(yc)
pt2 = self._horiz(x2), self._vert(y2)
points = []
for i in xrange(steps):
t = float(i) / steps
x = ((1-t)**2 * pt0[0]) + (2 * (1-t) * t * pt1[0]) + (t**2 * pt2[0])
y = ((1-t)**2 * pt0[1]) + (2 * (1-t) * t * pt1[1]) + (t**2 * pt2[1])
points.extend((x, y))
points.extend(pt2)
self._ctx.line(points, pen)
self._valid = False

def rect(self, x, y, width, height, stroke=(0.0, 0.0, 0.0, 1.0), thickness=1.0, fill=None):
"""Draw a rectangle with the upper left point at x,y."""
self._verify_context()
stroke = self._handle_color(stroke)
pen = aggdraw.Pen(stroke[:3], thickness, stroke[3])
brush = None
if fill:
fill = self._handle_color(fill)
brush = aggdraw.Brush(fill[:3], fill[3])
self._ctx.rectangle((self._horiz(x), self._vert(y), self._horiz(x + width), self._vert(y + height)), pen, brush)
self._valid = False

def arc(self, center_x, center_y, radius_x, radius_y=None, start=0, end=360, stroke=(0.0, 0.0, 0.0, 1.0), thickness=1.0, fill=None):
""" Draw an arc / ellipse / circle / pieslice. 'start' and 'end' are angles in degrees; 0 is 3:00 and degrees move clockwise.
Specify both radius_x and radius_y to make ellipses; fill to make a pie slice.
"""
self._verify_context()
stroke = self._handle_color(stroke)
pen = aggdraw.Pen(stroke[:3], thickness, stroke[3])
brush = None
if not radius_y:
radius_y = radius_x
if fill:
fill = self._handle_color(fill)
brush = aggdraw.Brush(fill[:3], fill[3])
coords = (self._horiz(center_x - radius_x), self._vert(center_y - radius_y), self._horiz(center_x + radius_x), self._vert(center_y + radius_y))
if self._flip:
coords = coords[0], coords[3], coords[2], coords[1]
else:
tmp = 360 - start
start = 360 - end
end = tmp
if start == 0 and end == 360:
self._ctx.ellipse(coords, pen, brush)
elif fill:
self._ctx.pieslice(coords, start, end, pen, brush)
else:
self._ctx.arc(coords, start, end, pen)
self._valid = False

def spiral(self, center_x, center_y, radius_x, radius_y=None, start_degrees=0, end_degrees=5*360, stroke=(0.0, 0.0, 0.0, 1.0), thickness=1.0, resolution=0.01, clockwise=True):
"""Draws spirals"""
self._verify_context()
stroke = self._handle_color(stroke)
pen = aggdraw.Pen(stroke[:3], thickness, stroke[3])
center = self._horiz(center_x), self._vert(center_y)
if not radius_y:
radius_y = radius_x
turns = float(end_degrees) / 360
radius_x /= math.pi * 2.0 * turns
radius_y /= math.pi * 2.0 * turns
points = []
t = math.pi * 2.0 * (float(start_degrees) / 360)
while t < math.pi * 2.0 * turns:
x = (1 if clockwise else -1) * (t * math.cos(t) * radius_x) + center[0]
y = (t * math.sin(t) * radius_y) + center[1]
points.extend((x, y))
t += resolution
self._ctx.line(points, pen)
self._valid = False

def text(self, text, font, x, y, stroke=(0.0, 0.0, 0.0, 1.0), size=18, center=False):
"""Draw text at x,y. 'font' should be the path to a TrueType font."""
self._render()
self._ctx = None # it's necessary to kill aggdraw before using Draw
draw = ImageDraw.Draw(self._image)
font = ImageFont.truetype(font, size)
size = draw.textsize(text, font=font)
if center:
x -= size[0] * 0.5
y -= size[1] * 0.5
stroke = self._handle_color(stroke)
draw.text((self._horiz(x), self._vert(y)), text, font=font, fill=stroke[:3])

def blur(self):
"""Apply a blur filter to the drawing context."""
self._render()
self._image = self._image.filter(ImageFilter.BLUR)
self._ctx = None

def smooth(self):
"""Apply a smoothing filter to the drawing context."""
self._render()
self._image = self._image.filter(ImageFilter.SMOOTH)
self._ctx = None

def sharpen(self):
"""Apply a sharpen filter to the drawing context."""
self._render()
self._image = self._image.filter(ImageFilter.SHARPEN)
self._ctx = None

def contrast(self): # todo: autocontrast, except if there is an argument, then adjust contrast
"""Apply autocontrast to the drawing context."""
if self.mode != 'RGB':
raise Exception("RGB mode required for 'contrast'")
self._render()
self._image = ImageOps.autocontrast(self._image)
self._ctx = None

def show(self):
"""Display the drawing context."""
import cv
if not self._valid:
self._render()
self.image.show()

def frame(self):
"""Show an animation loop via OpenCV"""
if self.mode != 'RGB':
raise Exception("RGB mode required for 'frame'")
if not self._valid:
self._render()
import cv
cv.ShowImage("drawing", pil_to_ipl(self.image))
key = cv.WaitKey(1)

def clear(self):
if self.mode != 'RGBA' or self.background[3] == 1.0:
self.rect(0, 0, self.width, self.height, thickness=0.0, fill=self.background)
else:
self._image = Image.new(self.mode, self._image.size, self._handle_color(self.background))
self._ctx = None
self._valid = True



def pil_to_ipl(pil_image):
"""Convert a PIL image to ipl (OpenCV)"""
import cv
cv_image = cv.CreateImageHeader(pil_image.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_image, pil_image.rotate(180).tostring()[::-1])
return cv_image


if __name__ == "__main__":

# rainbow ellipse specified relatively and in hsv
ctx = Context(640, 480, (0, 0, 0), relative=True, hsv=True)
for i in xrange(0, 100):
v = (100 - i) / 100.0
ctx.arc(0.5, 0.5, v, 0.75 * v, fill=(v, 1.0, 1.0), thickness=0)
ctx.blur()
ctx.show()

# random rects, animated
from random import random
ctx = Context(640, 480)
while True:
x, y = random() * ctx.width, random() * ctx.height
width, height = random() * 200, random() * 200
fill = random(), random(), random(), random()
ctx.rect(x, y, width, height, fill=fill)
ctx.frame()


Change log

9b716686758a by house on Apr 19, 2012   Diff
added centering to drawing
Go to: 
Project members, sign in to write a code review

Older revisions

bcb88d192584 by house on Apr 18, 2012   Diff
drawing notes
2d02644477c4 by house on Apr 16, 2012   Diff
added cability of handling
transparency
1beb2f598049 by house on Mar 18, 2012   Diff
made safe for dreamhost
All revisions of this file

File info

Size: 13032 bytes, 329 lines
Powered by Google Project Hosting