#!/usr/bin/env python # #Copyright (c) 2007 Andrew Walkingshaw # #Permission is hereby granted, free of charge, to any person #obtaining a copy of this software and associated documentation #files (the "Software"), to deal in the Software without #restriction, including without limitation the rights to use, #copy, modify, merge, publish, distribute, sublicense, and/or sell #copies of the Software, and to permit persons to whom the #Software is furnished to do so, subject to the following #conditions: # #The above copyright notice and this permission notice shall be #included in all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES #OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #OTHER DEALINGS IN THE SOFTWARE. import sys, random, Image, ImageDraw, ImageFont, ImageEnhance class Photo(object): def __init__(self, img): if isinstance(img, basestring): self.image = Image.open(imgfile) else: self.image = img self.pix = self.image.load() self.xsize, self.ysize = self.image.size def calcneighbours(self, px, py): xn = [px-5, px-4, px-3, px-2, px-1, px, px+1, px+2, px+3, px+4, px+5] yn = [py-5, py-4, py-3, py-2, py-1, py, py+1, py+2, py+3, py+4, py+5] neighbours = [(x, y) for x in xn for y in yn] return neighbours def mutatex(self, step): # pick a point py, px = divmod(step, self.xsize) py = py-1 # to get the indexing right # self.pix[px,py] = (0,0,0) neighbours = self.calcneighbours(px,py) + [(px,py)] r, g, b = 0,0,0 validneighbours = [] for p in neighbours: try: ar,ag,ab = self.pix[p[0],p[1]] r+=ar; g+=ag; b+=ab validneighbours.append(p) except IndexError: pass r = r/len(validneighbours) g = g/len(validneighbours) b = b/len(validneighbours) for p in validneighbours: self.pix[p[0], p[1]] = (r,g,b) def mutatey(self, step): # pick a point px, py = divmod(step, self.ysize) px = px-1 # to get the indexing right # self.pix[px,py] = (0,0,0) neighbours = self.calcneighbours(px,py) + [(px,py)] r, g, b = 0,0,0 validneighbours = [] for p in neighbours: try: ar,ag,ab = self.pix[p[0],p[1]] r+=ar; g+=ag; b+=ab validneighbours.append(p) except IndexError: pass r = r/len(validneighbours) g = g/len(validneighbours) b = b/len(validneighbours) for p in validneighbours: self.pix[p[0], p[1]] = (r,g,b) def render(self): return self.image def test(filename=None, format="PNG"): steps = 187000 stepsize = 440 g = Photo("c:\Documents and Settings\Andrew Walkingshaw\Desktop\cablefront.jpg") img = g.render() img.save("%s%s.png" % (filename,"0000"), format) for s in xrange(1, steps): g.mutatex(s) if s%stepsize == 0: step = s/stepsize; print step sn = "0"*(4-len(str(step)))+str(step) img = g.render() img.save("%s%s.png" % (filename,sn), format) for s in range(steps+1, 2*steps): g.mutatey(s-steps) if s%stepsize == 0: step = s/stepsize; print step sn = "0"*(4-len(str(step)))+str(step) img = g.render() img.save("%s%s.png" % (filename,sn), format) if __name__ == "__main__": test(filename="cablecar2/")