My favorites | Sign in
Project Logo
                
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
from sga import sgaGenotype
from PIL import Image
import aggdraw as ad

def ConvertToImage(gen,n):
bk = (255,255,255)
im = Image.new("RGB",(200,200),bk)
draw = ad.Draw(im)
for gr in gen.Lgroups:
br = ad.Brush((gr[10].Genes[n],gr[11].Genes[n],gr[12].Genes[n]),127)
draw.polygon([(gr[0].Genes[n] if i%2==0 else gr[1].Genes[n]) + \
loc.Genes[n] for i,loc in enumerate(gr[2:10])], None, br)
draw.flush()
return im

def MeasureColorDifference(gen,n):
global data
dif = 0
for co,cg in zip(data,list(ConvertToImage(gen,n).getdata())):
for x,y in zip(co,cg):
dif+=abs(x-y)
return dif

def BetterFoundNotify(gen, it):
global cycles
print "Iteration",it,"of",cycles,';','Fitness',gen.Fitness[2]
ConvertToImage(gen,2).save('/tmp/0000'+str(it)+'.png')

data = list(Image.open("LenaC.png").getdata()) # 200x200 pixels Lena image
cycles = 100000 # How much iterations do we try
dxy = 50

gen = sgaGenotype([range(dxy,200-dxy),range(dxy,200-dxy) # polygon center x,y coordinates
,range(-dxy,dxy+1),range(-dxy,dxy+1) # polygon 1 point dx,dy \
,range(-dxy,dxy+1),range(-dxy,dxy+1) # polygon 2 point dx,dy \
,range(-dxy,dxy+1),range(-dxy,dxy+1) # polygon 3 point dx,dy \
,range(-dxy,dxy+1),range(-dxy,dxy+1) # polygon 4 point dx,dy \
, [x for x in range(220) if x%51 == 0] # polygon fill color R component\
, [x for x in range(220) if x%51 == 0] # polygon fill color G component\
, [x for x in range(220) if x%51 == 0] ] # polygon fill color B component\
, 100 # Number of polygons\
, MeasureColorDifference # Fitness function, for determining picture quality\
, BetterFoundNotify # Notify function, informs when better solution is found\
, Minimize = True) # Do we need to minimize or maximize fitness function
gen.Evolve(cycles)
print "Done. Now you can go to drink coffee."
Show details Hide details

Change log

r18 by vasiliauskas.agnius on Feb 16, 2009   Diff
sga - art created by evolution strategy
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1829 bytes, 46 lines
Hosted by Google Code