Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
This is a script written by Randal S. Olson ( for the Traveling Salesman Portrait project.
More information on the project can be found on my blog:
Please check my project repository for information on how this script can be used and shared:
import os
import math
import matplotlib.pyplot as plt
import numpy as np
import urllib.request
from PIL import Image
from tsp_solver.greedy_numpy import solve_tsp
from scipy.spatial.distance import pdist, squareform
image_url = ''
image_path = 'Frankenstein.jpg'
if not os.path.exists(image_path):
urllib.request.urlretrieve(image_url, image_path)
original_image =
bw_image = original_image.convert('1', dither=Image.NONE)
bw_image_array = np.array(bw_image,
black_indices = np.argwhere(bw_image_array == 0)
chosen_black_indices = black_indices[np.random.choice(black_indices.shape[0], replace=False, size=10000)]
distances = pdist(chosen_black_indices)
distance_matrix = squareform(distances)
optimized_path = solve_tsp(distance_matrix)
optimized_path_points = [chosen_black_indices[x] for x in optimized_path]
plt.figure(figsize=(8, 10), dpi=100)
plt.plot([x[1] for x in optimized_path_points], [x[0] for x in optimized_path_points], color='black', lw=1)
plt.xlim(0, 600)
plt.ylim(0, 800)
plt.savefig('traveling-salesman-portrait.png', bbox_inches='tight')