Guest User

Voronoi

a guest
May 15th, 2015
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. """
  2. File format should be:
  3.  
  4. <width> <height>
  5. <x> <y> <r> <g> <b>
  6. <x> <y> <r> <g> <b>
  7. ...
  8.  
  9. All values should be integers, with rgb from 0-255.
  10. """
  11.  
  12. from collections import namedtuple
  13. import sys
  14. from PIL import Image
  15.  
  16. INPATH = "voronoi.txt"
  17. OUTPATH = "voronoi.png"
  18.  
  19. Point = namedtuple("Point", ["pos", "color"])
  20. points = []
  21.  
  22. with open(INPATH) as infile:
  23.     width, height = map(int, infile.readline().split())
  24.    
  25.     for line in infile:
  26.         x, y, r, g, b = map(int, line.split())
  27.         points.append(Point((x,y), (r,g,b)))
  28.  
  29. im = Image.new("RGB", (width, height))
  30.  
  31. def dist_partial(p1):
  32.     def dist(p2):
  33.         return ((p1[0] - p2.pos[0])**2 + (p1[1] - p2.pos[1])**2)**.5
  34.     return dist
  35.  
  36. for x in xrange(width):
  37.     for y in xrange(height):
  38.         closest = min(points, key=dist_partial((x, y)))
  39.         im.putpixel((x, y), closest.color)
  40.        
  41. im.save(OUTPATH)
Advertisement
Add Comment
Please, Sign In to add comment