Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import os, random, sys
  2. # if you're using Pillow use this
  3. from PIL import Image
  4. # if you're using PIL you can just import Image
  5. from collections import defaultdict
  6.  
  7.  
  8. # Build a table keyed by rgb tuples and returning a list of tuples consisting of the
  9. # color of each neighboring pixel and its relative position. For example ((39, 20, 13), (-1, -1))
  10. def buildTable( imageFilename ):
  11.  
  12.     im = Image.open(imageFilename)
  13.     table = defaultdict(list)
  14.     w, h = im.size
  15.     print w*h
  16.  
  17.     for x in xrange(w):
  18.         for y in xrange(h):
  19.             p = im.getpixel((x, y))
  20.             # for each of the pixel's neighbors
  21.             for x2 in xrange(-1, 2):
  22.                 for y2 in xrange(-1, 2):
  23.                     if x2 == 0 and y2 == 0:
  24.                         continue
  25.                     pn = im.getpixel( ((x+x2)%w, (y+y2)%h) )
  26.                     table[p].append((pn, (x2, y2)))
  27.  
  28.     print len(table)
  29.     #im.close()
  30.     return table
  31.  
  32. # Draw in the pixel and all of its neighbors, choose a random neighbor, repeat
  33. def markovImage4( imageFilename, width=512, height=512, table=None ):
  34.  
  35.     if not table:
  36.         table = buildTable(imageFilename)
  37.  
  38.     x = width/2
  39.     y = height/2
  40.     color = random.choice(table.keys())
  41.     im = Image.new("RGB", (width, height), color)
  42.     # This is a random walk so you're not guaranteed to hit every pixel
  43.     # but doing w*h iterations is enough to get most of them
  44.     for i in xrange( width*height ):
  45.  
  46.         im.putpixel((x, y), color)
  47.         neighbors = table[color]
  48.         n_neighbors = len(neighbors)
  49.         # Just grab a random point in the list of neighbors and take it
  50.         # and the next 7. They probably won't all be original neighbors.
  51.         start = random.randint(0, n_neighbors-1)
  52.         for j in xrange(start, start+8):
  53.             n = neighbors[j%n_neighbors]
  54.             color = n[0]
  55.             offset = n[1]
  56.             xo = (x+offset[0])%width
  57.             yo = (y+offset[1])%height
  58.             im.putpixel((xo, yo), color)
  59.         # Since we picked a random neighbor to start with we can just use
  60.         # j to pick the next point
  61.         n = neighbors[j%n_neighbors]
  62.         color = n[0]
  63.         # using a multiplier of 4 creates a grid
  64.         x = (x+3*n[1][0]) % width
  65.         y = (y+3*n[1][1]) % height
  66.  
  67.     outFilename = "markovImage4 - %s.png" % (imageFilename.split(".")[0])
  68.     out = open(outFilename, 'wb')
  69.     im.save(out, "PNG")
  70.     out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement