|
|
@ -9,13 +9,13 @@ from time import time |
|
|
|
|
|
|
|
|
|
|
|
rand = lambda: randint(0,200) |
|
|
|
quiet = False |
|
|
|
progress = False |
|
|
|
|
|
|
|
|
|
|
|
def print_stats(img): |
|
|
|
pixels = img.getdata() |
|
|
|
count = sum([1 for i in pixels if i == 0]) |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("{} black pixels over {} ({:.1%})".format(count, len(pixels), count/len(pixels))) |
|
|
|
|
|
|
|
|
|
|
@ -25,7 +25,7 @@ def get_lines(img): |
|
|
|
|
|
|
|
i, n = 0, len(pixels) * len(pixels) |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("Get lines:", end = "") |
|
|
|
for a in pixels: |
|
|
|
for b in pixels: |
|
|
@ -38,12 +38,12 @@ def get_lines(img): |
|
|
|
if is_in: |
|
|
|
lines.append(line) |
|
|
|
i += 1 |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rGet lines: {:.1%}".format(i / n), end = "") |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rGet lines: complete") |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("{} lines found".format(len(lines))) |
|
|
|
return lines |
|
|
|
|
|
|
@ -52,7 +52,7 @@ def removing_doubles(lines): |
|
|
|
results = [] |
|
|
|
i, n = 0, len(lines) |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("Remove duplicated lines:", end = "") |
|
|
|
for l in lines: |
|
|
|
s = sorted(l) |
|
|
@ -64,12 +64,12 @@ def removing_doubles(lines): |
|
|
|
if same == False: |
|
|
|
results.append(l) |
|
|
|
i += 1 |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rRemove double lines: {:.0%}".format(i / n), end = "") |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rRemove double lines: complete") |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("{} lines kept".format(len(results))) |
|
|
|
return results |
|
|
|
|
|
|
@ -78,7 +78,7 @@ def removing_useless(lines): |
|
|
|
results = [] |
|
|
|
i, n = 0, len(lines) |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("Remove useless lines:", end = "") |
|
|
|
for l in lines: |
|
|
|
inclusions = 0 |
|
|
@ -90,12 +90,12 @@ def removing_useless(lines): |
|
|
|
if inclusions == 0 or len(l) == 1: |
|
|
|
results.append((len(l), l)) |
|
|
|
i += 1 |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rRemove useless lines: {:.0%}".format(i / n), end = "") |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rRemove useless lines: complete") |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("{} lines kept".format(len(results))) |
|
|
|
return results |
|
|
|
|
|
|
@ -105,7 +105,7 @@ def get_best_solution(img, lines): |
|
|
|
results = [] |
|
|
|
n = len(px_left) |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("Draw:", end = "") |
|
|
|
while len(px_left): |
|
|
|
lines = [(sum([1 for p in l if p in px_left]) - len(l)/(2*max(img.size)), l) for n, l in lines] |
|
|
@ -113,12 +113,12 @@ def get_best_solution(img, lines): |
|
|
|
(p, line) = lines.pop() |
|
|
|
px_left = [p for p in px_left if p not in line] |
|
|
|
results.append((line[0], line[-1])) |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rDraw: {:.0%}".format(1 - len(px_left)/n), end="") |
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("\rDraw: complete") |
|
|
|
|
|
|
|
if not quiet: |
|
|
|
if progress: |
|
|
|
print("Solution found in {} lines".format(len(results))) |
|
|
|
return results |
|
|
|
|
|
|
@ -142,16 +142,16 @@ if __name__ == "__main__": |
|
|
|
|
|
|
|
parser = ArgumentParser(description='Generate the Multi DrawStat code for an image.') |
|
|
|
parser.add_argument('path', type=str, help='path of the image to process') |
|
|
|
parser.add_argument('-c', '--codeonly', action='store_true', help='return code only (override -i)') |
|
|
|
parser.add_argument('-d', '--draw', action='store_true', help='draw the result into a new file') |
|
|
|
parser.add_argument('-f', '--flip', action='store_true', help='flip image vertically (for inverted ViewWindow)') |
|
|
|
parser.add_argument('-i', '--info', action='store_true', help='print informative stats') |
|
|
|
parser.add_argument('-o', '--offset', nargs=2, default=(0, 0), help='offset for viewwindow. Default: (0, 0)') |
|
|
|
parser.add_argument('-p', '--progress', action='store_true', help='print progress info') |
|
|
|
parser.add_argument('-s', '--show', action='store_true', help='show the result') |
|
|
|
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
quiet = args.codeonly |
|
|
|
progress = args.progress |
|
|
|
|
|
|
|
try: |
|
|
|
image = Image.open(args.path) |
|
|
@ -166,7 +166,7 @@ if __name__ == "__main__": |
|
|
|
if args.flip: |
|
|
|
image = image.transpose(Image.FLIP_TOP_BOTTOM) |
|
|
|
|
|
|
|
if args.info and not quiet: |
|
|
|
if args.info: |
|
|
|
print_stats(image) |
|
|
|
lines = get_lines(image) |
|
|
|
lines = removing_doubles(lines) |
|
|
@ -185,6 +185,6 @@ if __name__ == "__main__": |
|
|
|
if args.show: |
|
|
|
export.show() |
|
|
|
|
|
|
|
if args.info and not quiet: |
|
|
|
if args.info: |
|
|
|
print("{} processed in {} lines ({:.3}s)".format(args.path, len(lines), time() - start)) |
|
|
|
print(code) |