import PIL.Image as Image import PIL.ImageDraw as ImageDraw from scikits.delaunay import delaunay import numpy import math print "Collecting point cloud...", image = Image.open('output.png').convert('1') step = 8 points_x = [] points_y = [] for y in range(0, image.size[1], step): for x in range(0, image.size[0], step): if image.getpixel((x, y)) != 0: points_x.append(x) points_y.append(y) fpoints_x = numpy.array(points_x, dtype = 'float') fpoints_y = numpy.array(points_y, dtype = 'float') print "done" print "Delaunay...", circumcenters, edges, tri_points, tri_neighbours = delaunay(fpoints_x, fpoints_y) print "done" print "Drawing output...", output = Image.new('RGB', image.size) draw = ImageDraw.Draw(output) for indexes in tri_points: points = [(points_x[i], points_y[i]) for i in indexes] points.append(points[0]) lengths = [] for i in range(3): x1, y1 = points[i] x2, y2 = points[i + 1] lengths.append((x1 - x2) ** 2 + (y1 - y2) ** 2) avglength = math.sqrt(sum(lengths) / 3.) draw.polygon(points, fill = (int(255 * (avglength < 30)), 0, 0)) print "done" output.save('output2.png')