My favorites | Sign in
Project Logo
                
Changes to /trunk/Python/lena_line.py
r0 vs. r18   Edit
  Compare: vs.   Format:
Revision r18
Go to: 
Project members, sign in to write a code review
/trunk/Python/lena_line.py /trunk/Python/lena_line.py   r18
  1 from sga import sgaGenotype
  2 from PIL import Image, ImageDraw
  3
  4 def ConvertToImage(gen,n):
  5 im = Image.new("L",(200,200),255)
  6 draw = ImageDraw.Draw(im)
  7 for gr in gen.Lgroups:
  8 draw.line([gr[0].Genes[n], \
  9 gr[1].Genes[n], \
  10 gr[0].Genes[n] + gr[2].Genes[n], \
  11 gr[1].Genes[n] + gr[3].Genes[n]], fill = gr[4].Genes[n], width = gr[5].Genes[n] )
  12 return im
  13
  14 def MeasureColorDifference(gen,n):
  15 global data
  16 dif = 0
  17 for co,cg in zip(data,list(ConvertToImage(gen,n).getdata())):
  18 dif+=abs(co-cg)
  19 return dif
  20
  21 def BetterFoundNotify(gen, it):
  22 global cycles
  23 print "Iteration",it,"of",cycles,';','Fitness',gen.Fitness[2]
  24 ConvertToImage(gen,2).save('/tmp/0000'+str(it)+'.png')
  25
  26 data = list(Image.open("LenaG.png").getdata()) # 200x200 pixels Lena image
  27 cycles = 100000 # How much iterations do we try
  28 dxy = 20
  29
  30 gen = sgaGenotype([range(dxy,200-dxy),range(dxy,200-dxy) # line start x,y \
  31 ,range(-dxy,dxy+1),range(-dxy,dxy+1) # line dx,dy \
  32 , [x for x in range(220) if x%5 == 0] # line grayscale color\
  33 , range(1,8) ] # line width in pixels\
  34 , 200 # Number of lines\
  35 , MeasureColorDifference # Fitness function, for determining picture quality\
  36 , BetterFoundNotify # Notify function, informs when better solution is found\
  37 , Minimize = True) # Do we need to minimize or maximize fitness function
  38 gen.Evolve(cycles)
  39 print "Done. Now you can go to drink coffee."
Hosted by Google Code