forked from matthras/tsp-art-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw-tsp-path-svg.py
62 lines (56 loc) · 1.97 KB
/
draw-tsp-path-svg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import cairo
from PIL import Image
# Change these file names to the relevant files.
ORIGINAL_IMAGE = "example-output/smileyface-inverted.png"
IMAGE_TSP = "example-output/smileyface-inverted-1024-stipple.tsp"
IMAGE_CYC = "example-output/smileyface-inverted-1024-stipple.cyc"
def obtain_list_of_nodes():
list_of_nodes = []
with open(IMAGE_TSP) as f:
for _ in range(6):
next(f)
for line in f:
i,x,y = line.split()
list_of_nodes.append((int(float(x)),int(float(y))))
return list_of_nodes
def obtain_concorde_solution(list_of_nodes):
tsp_path = []
with open(IMAGE_CYC) as g:
for line in g:
tsp_path.append(list_of_nodes[int(line)])
tsp_path.append(list_of_nodes[0]) # The .cyc file does not include the starting node
return tsp_path
def draw_svg(tsp_path):
im = Image.open(ORIGINAL_IMAGE)
WIDTH, HEIGHT = im.size
FINAL_IMAGE_SVG = IMAGE_TSP.replace("-stipple.tsp", "-tsp.svg")
#surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,WIDTH,HEIGHT)
surface = cairo.SVGSurface(FINAL_IMAGE_SVG,WIDTH,HEIGHT)
ctx = cairo.Context(surface)
#ctx.scale(WIDTH,HEIGHT) # Not sure about this, seems redundant if image dimensions are already specified by surface
# Transform to normal cartesian coordinate system
m = cairo.Matrix(yy=-1, y0=HEIGHT)
ctx.transform(m)
# Paint the background white
ctx.save()
ctx.set_source_rgb(1,1,1)
ctx.paint()
ctx.restore()
# Draw lines
ctx.move_to(tsp_path[0][0], tsp_path[0][1])
for node in range(1,len(tsp_path)):
ctx.line_to(tsp_path[node][0],tsp_path[node][1])
ctx.save()
ctx.set_source_rgb(0,0,0)
ctx.set_line_width(1)
ctx.stroke_preserve()
ctx.restore()
#FINAL_IMAGE_PNG = IMAGE_TSP.replace("-stipple.tsp", "-tsp.png")
#surface.write_to_png(FINAL_IMAGE)
surface.finish()
def main():
list_of_nodes = obtain_list_of_nodes()
tsp_path = obtain_concorde_solution(list_of_nodes) # Later change to if-no-concorde, then use OR-tools
draw_svg(tsp_path)
if __name__ == '__main__':
main()