forked from matthras/tsp-art-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw-tsp-path-concorde.py
35 lines (28 loc) · 1.1 KB
/
draw-tsp-path-concorde.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
# Copyright Matthew Mack (c) 2020 under CC-BY 4.0: https://creativecommons.org/licenses/by/4.0/
from PIL import Image, ImageDraw
import os.path
ORIGINAL_IMAGE = "images/smileyface-inverted.png"
IMAGE_TSP = "images/smileyface-inverted-1024-stipple.tsp"
IMAGE_CYC = "images/smileyface-inverted-1024-stipple.cyc"
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))))
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])
original_image = Image.open(ORIGINAL_IMAGE)
width, height = original_image.size
tsp_image = Image.new("RGBA",(width,height),color='white')
tsp_image_draw = ImageDraw.Draw(tsp_image)
#tsp_image_draw.point(tsp_path,fill='black')
tsp_image_draw.line(tsp_path,fill='black',width=1)
tsp_image = tsp_image.transpose(Image.FLIP_TOP_BOTTOM)
FINAL_IMAGE = IMAGE_TSP.replace("-stipple.tsp", "-tsp.png")
tsp_image.save(FINAL_IMAGE)
print("TSP solution has been drawn and can be viewed at", FINAL_IMAGE)