X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/841662dc25b40dce151a0bf7a024e55682028f18..04c7dfcf51881df1f6a2058c8a74c179ab3aee13:/src/manual.py?ds=inline diff --git a/src/manual.py b/src/manual.py index f6f3807..2eb4ac5 100644 --- a/src/manual.py +++ b/src/manual.py @@ -1,8 +1,8 @@ """Manual grid selection module""" -import pygame -import ImageDraw from math import sqrt, acos, copysign +from PIL import ImageDraw +import pygame from geometry import l2ad, line, intersection @@ -22,19 +22,52 @@ class Screen: self._screen.blit(pg_img, (0, 0)) pygame.display.flip() +def dst((x1, y1), (x2, y2)): + return (x1 - x2) ** 2 + (y1 - y2) ** 2 + +def display_instr(): + + while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + raise UserQuitError + if event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.KEYDOWN: + return + def find_lines(im_orig): # TODO rename, refactor, comment im = im_orig.copy() - screen = Screen(im.size) + screen = Screen((620, 350)) + + font = pygame.font.Font(None, 25) + instructions = ["Imago manual mode", "", + "To set the grid position, click on the corners of the grid. Once you mark", + "all four corners, the grid will appear. To adjust it, just click on the new", + "position and the nearest corner will move there. Once you are content", + "with the alignment, press any key on your keyboard or close the window.", + "", "", "", + "Press any key to continue."] + y = 10 + for i in instructions: + text1 = font.render(i, True, [128, 255, 128]) + screen._screen.blit(text1, [10, y]) + y += 25 + + pygame.display.flip() + + display_instr() + + pygame.display.set_mode(im.size) clock = pygame.time.Clock() draw = ImageDraw.Draw(im) hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1), fill=(255, 64, 64)) corners = [] - color = (64, 64, 255) + color = (32, 255, 32) line_width = 1 lines_r = [] @@ -47,43 +80,50 @@ def find_lines(im_orig): else: raise UserQuitError if event.type == pygame.MOUSEBUTTONDOWN: + np = pygame.mouse.get_pos() if len(corners) >= 4: - corners = [] + corners.sort(key=lambda p: dst(p, np)) + corners = corners[1:] + corners.append(np) + (x, y) = corners[-1] + draw.line((x-2, y, x + 2, y), fill=color) + draw.line((x, y+2, x, y-2), fill=color) + if len(corners) == 4: im = im_orig.copy() draw = ImageDraw.Draw(im) - - if len(corners) < 4: - corners.append(pygame.mouse.get_pos()) - draw.point(corners[:-1], fill=color) - if len(corners) == 4: + try: l_vert, l_hor = lines(corners) - for l in l_vert: - draw.line(l, fill=color, width=line_width) - for l in l_hor: - draw.line(l, fill=color, width=line_width) - # TODO sort by distance - l_vert.sort() - l_hor.sort() - for i in [3, 9, 15]: - for j in [3, 9, 15]: - hoshi(intersection(line(l_vert[i][0], l_vert[i][1]), - line(l_hor[j][0], l_hor[j][1]))) - lines_r = [[l2ad(l, im.size) for l in l_vert], - [l2ad(l, im.size) for l in l_hor]] + except Exception: + corners = corners[:-1] + for l in l_vert: + draw.line(l, fill=color, width=line_width) + for l in l_hor: + draw.line(l, fill=color, width=line_width) + # TODO sort by distance + #l_vert.sort() + #l_hor.sort() + #for i in [3, 9, 15]: + # for j in [3, 9, 15]: + # hoshi(intersection(line(l_vert[i][0], l_vert[i][1]), + # line(l_hor[j][0], l_hor[j][1]))) + lines_r = [[l2ad(l, im.size) for l in l_vert], + [l2ad(l, im.size) for l in l_hor]] screen.display_picture(im) clock.tick(15) def lines(corners): # TODO Error on triangle + corners.sort() # TODO does this help? + # TODO refactor this vvv cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in corners[1:]] cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) * sqrt(b[0] **2 + b[1] ** 2)), a[0] * b[1] - b[0] * a[1], c) for a, b, c in cor_d] cor_d = sorted([(copysign(acos(min(a, 1)), b), c) for a, b, c in cor_d]) corners = [corners[0]] + [c for _, c in cor_d] - return (_lines(corners, 0) + [(corners[0], corners[3]), - (corners[1], corners[2])], + return (_lines(corners, 0) + + [(corners[0], corners[3]), (corners[1], corners[2])], _lines(corners[1:4] + [corners[0]], 0) + [(corners[0], corners[1]), (corners[2], corners[3])]) @@ -91,8 +131,8 @@ def _lines(corners, n): # TODO what is this? if n == 0: x = half_line(corners) - return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] + - _lines([x[0], corners[1], corners[2], x[1]], n + 1)) + return (_lines([corners[0], x[0], x[1], corners[3]], 1) + [x] + + _lines([x[0], corners[1], corners[2], x[1]], 1)) else: x = half_line(corners) c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))