source: samples/sample-gradient.py@ 387

Last change on this file since 387 was 304, checked in by Rick van der Zwet, 14 years ago

Sample python gradient implementation.

  • Property svn:executable set to *
File size: 1.4 KB
RevLine 
[304]1#!/usr/bin/env python
2#
3# Example on how to generate multiple linear gradiant circles in one picture,
4# adding the data if present.
5#
6# Rick van der Zwet <info@rickvanderzwet.nl>
7
8
9from PIL import Image
10import ImageDraw
11import numpy as np
12
13
14def make_circle(draw, center, radius,colour=(0,255,0)):
15 """ Cicle gradient is created by creating smaller and smaller cicles """
16 (center_x, center_y) = center
17 for i in range(0,radius):
18 draw.ellipse(
19 (center_x - radius + i,
20 center_y - radius + i,
21 center_x + radius - i,
22 center_y + radius - i
23 ),
24 colour +(255 * i/(radius * 2),)
25 )
26
27
28class Picture():
29 im = None
30 def __init__(self, method, size):
31 self.im = Image.new(method, size)
32
33 def save(self,filename):
34 self.im.save(filename)
35
36 def add_circle(self, center, radius, colour):
37 """ Adding a new cicle is a matter of creating a new one in a empty layer
38 and merging it with the current one """
39
40 im_new = Image.new("RGBA", self.im.size)
41 draw = ImageDraw.Draw(im_new)
42 make_circle(draw, center, radius, colour)
43
44 data = np.array(self.im)
45 data2 = np.array(im_new)
46
47 # Add channels to make new images
48 data3 = data + data2
49
50 self.im = Image.fromarray(data3)
51
52
53im = Picture("RGBA",(200,200))
54
55im.add_circle((50,50),100,(0,255,0))
56im.add_circle((150,150),100,(255,0,0))
57
58filename = 'sample-gradient.png'
59im.save(filename)
60print "#INFO Output saved as '%s'" % filename
Note: See TracBrowser for help on using the repository browser.