[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 |
|
---|
| 9 | from PIL import Image
|
---|
| 10 | import ImageDraw
|
---|
| 11 | import numpy as np
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | def 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 |
|
---|
| 28 | class 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 |
|
---|
| 53 | im = Picture("RGBA",(200,200))
|
---|
| 54 |
|
---|
| 55 | im.add_circle((50,50),100,(0,255,0))
|
---|
| 56 | im.add_circle((150,150),100,(255,0,0))
|
---|
| 57 |
|
---|
| 58 | filename = 'sample-gradient.png'
|
---|
| 59 | im.save(filename)
|
---|
| 60 | print "#INFO Output saved as '%s'" % filename
|
---|