#!/usr/bin/env python # # Example on how to generate multiple linear gradiant circles in one picture, # adding the data if present. # # Rick van der Zwet from PIL import Image import ImageDraw import numpy as np def make_circle(draw, center, radius,colour=(0,255,0)): """ Cicle gradient is created by creating smaller and smaller cicles """ (center_x, center_y) = center for i in range(0,radius): draw.ellipse( (center_x - radius + i, center_y - radius + i, center_x + radius - i, center_y + radius - i ), colour +(255 * i/(radius * 2),) ) class Picture(): im = None def __init__(self, method, size): self.im = Image.new(method, size) def save(self,filename): self.im.save(filename) def add_circle(self, center, radius, colour): """ Adding a new cicle is a matter of creating a new one in a empty layer and merging it with the current one """ im_new = Image.new("RGBA", self.im.size) draw = ImageDraw.Draw(im_new) make_circle(draw, center, radius, colour) data = np.array(self.im) data2 = np.array(im_new) # Add channels to make new images data3 = data + data2 self.im = Image.fromarray(data3) im = Picture("RGBA",(200,200)) im.add_circle((50,50),100,(0,255,0)) im.add_circle((150,150),100,(255,0,0)) filename = 'sample-gradient.png' im.save(filename) print "#INFO Output saved as '%s'" % filename