|
|
|
@ -21,13 +21,16 @@ from time import time
|
|
|
|
|
rand = lambda: randint(0,200)
|
|
|
|
|
progress = False
|
|
|
|
|
|
|
|
|
|
def info(*args, **kwargs):
|
|
|
|
|
"""Like print, but if progress flag is enabled"""
|
|
|
|
|
if progress:
|
|
|
|
|
print(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def print_stats(img):
|
|
|
|
|
"""Print number and percentage of black pixels"""
|
|
|
|
|
pixels = img.getdata()
|
|
|
|
|
count = sum([1 for i in pixels if i == 0])
|
|
|
|
|
if progress:
|
|
|
|
|
print("{} black pixels over {} ({:.1%})".format(count, len(pixels), count/len(pixels)))
|
|
|
|
|
info("{} black pixels over {} ({:.1%})".format(count, len(pixels), count/len(pixels)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_lines(img):
|
|
|
|
@ -38,8 +41,7 @@ def get_lines(img):
|
|
|
|
|
|
|
|
|
|
i, n = 0, len(pixels) * len(pixels)
|
|
|
|
|
|
|
|
|
|
if progress:
|
|
|
|
|
print("Get lines:", end = "")
|
|
|
|
|
info("Get lines:", end = "")
|
|
|
|
|
# for each pair of pixels, get the line
|
|
|
|
|
for a in pixels:
|
|
|
|
|
for b in pixels:
|
|
|
|
@ -51,12 +53,10 @@ def get_lines(img):
|
|
|
|
|
lines.add(line)
|
|
|
|
|
lines_uniq.add(line_uniq)
|
|
|
|
|
i += 1
|
|
|
|
|
if progress:
|
|
|
|
|
print("\rGet lines: {:.1%}".format(i / n), end = "")
|
|
|
|
|
info("\rGet lines: {:.1%}".format(i / n), end = "")
|
|
|
|
|
|
|
|
|
|
if progress:
|
|
|
|
|
print("\rGet lines: complete")
|
|
|
|
|
print("{} lines found".format(len(lines)))
|
|
|
|
|
info("\rGet lines: complete")
|
|
|
|
|
info("{} lines found".format(len(lines)))
|
|
|
|
|
return list(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -65,8 +65,7 @@ def removing_useless(lines):
|
|
|
|
|
results = []
|
|
|
|
|
n = len(lines)
|
|
|
|
|
|
|
|
|
|
if progress:
|
|
|
|
|
print("Remove useless lines:", end = "")
|
|
|
|
|
info("Remove useless lines:", end = "")
|
|
|
|
|
# for each line, see if there is a line that contains every pixel of it
|
|
|
|
|
lines_set = [ set(l) for l in lines ]
|
|
|
|
|
|
|
|
|
@ -82,13 +81,10 @@ def removing_useless(lines):
|
|
|
|
|
# TODO: do some tests to see if it's worth or not
|
|
|
|
|
if inclusions == 0 or len(l) == 1:
|
|
|
|
|
results.append((len(l), lines[i], l))
|
|
|
|
|
if progress:
|
|
|
|
|
print("\rRemove useless lines: {:.1%}".format(i / n), end = "")
|
|
|
|
|
if progress:
|
|
|
|
|
print("\rRemove useless lines: complete")
|
|
|
|
|
info("\rRemove useless lines: {:.1%}".format(i / n), end = "")
|
|
|
|
|
info("\rRemove useless lines: complete")
|
|
|
|
|
|
|
|
|
|
if progress:
|
|
|
|
|
print("{} lines kept".format(len(results)))
|
|
|
|
|
info("{} lines kept".format(len(results)))
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -99,8 +95,7 @@ def get_best_solution(img, lines):
|
|
|
|
|
results = []
|
|
|
|
|
n = len(px_left)
|
|
|
|
|
|
|
|
|
|
if progress:
|
|
|
|
|
print("Draw:", end = "")
|
|
|
|
|
info("Draw:", end = "")
|
|
|
|
|
# while the entier image has not been drown
|
|
|
|
|
while px_left:
|
|
|
|
|
# define the length of lines
|
|
|
|
@ -113,13 +108,10 @@ def get_best_solution(img, lines):
|
|
|
|
|
# define the pixels that are not covered by any lines
|
|
|
|
|
px_left = px_left.difference(line_set)
|
|
|
|
|
results.append((line[0], line[-1]))
|
|
|
|
|
if progress:
|
|
|
|
|
print("\rDraw: {:.0%}".format(1 - len(px_left)/n), end="")
|
|
|
|
|
if progress:
|
|
|
|
|
print("\rDraw: complete")
|
|
|
|
|
info("\rDraw: {:.0%}".format(1 - len(px_left)/n), end="")
|
|
|
|
|
info("\rDraw: complete")
|
|
|
|
|
|
|
|
|
|
if progress:
|
|
|
|
|
print("Solution found in {} lines".format(len(results)))
|
|
|
|
|
info("Solution found in {} lines".format(len(results)))
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|