Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- File format should be:
- <width> <height>
- <x> <y> <r> <g> <b>
- <x> <y> <r> <g> <b>
- ...
- All values should be integers, with rgb from 0-255.
- """
- from collections import namedtuple
- import sys
- from PIL import Image
- INPATH = "voronoi.txt"
- OUTPATH = "voronoi.png"
- Point = namedtuple("Point", ["pos", "color"])
- points = []
- with open(INPATH) as infile:
- width, height = map(int, infile.readline().split())
- for line in infile:
- x, y, r, g, b = map(int, line.split())
- points.append(Point((x,y), (r,g,b)))
- im = Image.new("RGB", (width, height))
- def dist_partial(p1):
- def dist(p2):
- return ((p1[0] - p2.pos[0])**2 + (p1[1] - p2.pos[1])**2)**.5
- return dist
- for x in xrange(width):
- for y in xrange(height):
- closest = min(points, key=dist_partial((x, y)))
- im.putpixel((x, y), closest.color)
- im.save(OUTPATH)
Advertisement
Add Comment
Please, Sign In to add comment