Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Image
- def randomSeedFill(imageFileName, n_centers = 256 ):
- origImage = Image.open(imageFileName)
- w, h = origImage.size
- centers = [ (random.randint(0, w-1), random.randint(0, h-1)) for i in xrange(n_centers) ]
- colors = [origImage.getpixel(cen) for cen in centers]
- im = multiFloodFillQShuffle(w, h, centers, colors)
- outFilename = "randomSeedFill shuffle - %s - n_centers %d.png" % (imageFileName.split(".")[0], n_centers)
- out = open(outFilename, 'wb')
- im.save(out, "PNG")
- out.close()
- # "Parallel" queue-based flood fill. Shuffles the order neighbors are visited.
- def multiFloodFillQShuffle( w, h, pixelAddresses, replacementColors ):
- im = Image.new("RGB", (w, h))
- # keeps track of which pixels have been filled
- processed = {}
- for x in xrange(w):
- for y in xrange(h):
- processed[(x, y)] = False
- # each seed gets its own queue
- qs = [[p] for p in pixelAddresses]
- sanity = 0
- while sum([len(q) for q in qs]) > 0:
- # while loops make me nervous...
- sanity += 1
- if sanity > w*h:
- print "insanity"
- break
- for i in xrange(len(qs)):
- q = qs[i]
- if len(q) == 0:
- continue
- replacementColor = replacementColors[i]
- x, y = q.pop()
- x = x%w
- y = y%h
- if not processed[(x, y)]:
- im.putpixel((x, y), replacementColor)
- processed[(x, y)] = True
- xp = (x+1)%w; xm = (x-1)%w; yp = (y+1)%h; ym = (y-1)%h
- neighbors = [(xm, y), (x, ym), (x, yp), (xp, y)]
- # shuffle the directions then add each neighbor to the queue
- # (or don't shuffle to get a striped effect)
- # (or cycle through through them for a pattern, or...)
- random.shuffle(neighbors)
- for n in neighbors:
- if not processed[n]:
- q.append(n)
- return im
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement