1 """Manual grid selection module"""
4 import Image, ImageDraw
6 class UserQuitError(Exception):
10 def __init__(self, res):
12 pygame.display.set_mode(res)
13 pygame.display.set_caption("Go image capture")
14 self._screen = pygame.display.get_surface()
16 def display_picture(self, im):
17 pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
18 self._screen.blit(pg_img, (0,0))
21 def find_lines(im_orig):
25 screen = Screen(im.size)
28 clock = pygame.time.Clock()
29 draw = ImageDraw.Draw(im)
34 for event in pygame.event.get():
35 if event.type == pygame.QUIT:
38 if event.type == pygame.MOUSEBUTTONDOWN:
42 draw = ImageDraw.Draw(im)
45 corners.append(pygame.mouse.get_pos())
46 draw.point(corners[:-1], fill=color)
48 draw.line((corners[0], corners[1]), fill=color,
50 draw.line((corners[1], corners[2]), fill=color,
52 draw.line((corners[2], corners[3]), fill=color,
54 draw.line((corners[3], corners[0]), fill=color,
56 l1 = half_line(corners)
57 draw.line(l1, fill=color, width=line_width)
58 l2 = half_line(corners[1:4] + [corners[0]])
59 draw.line(l2, fill=color, width=line_width)
61 draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
64 screen.display_picture(im)
67 def half_line(corners):
69 d1 = intersection(line(corners[0], corners[3]),
70 line(corners[1], corners[2]))
71 p1 = intersection(line(c,d1), line(corners[0],
73 p2 = intersection(line(c,d1), line(corners[2],
78 return intersection(line(corners[0], corners[2]), line(corners[1],
81 a = float(x[1] - y[1])
82 b = float(y[0] - x[0])
83 c = a * y[0] + b * y[1]
86 def intersection(p, q):
87 det = p[0] * q[1] - p[1] * q[0]
90 return (int(round((q[1] * p[2] - p[1] * q[2]) / det)), int(round((p[0] *
91 q[2] - q[0] * p[2]) / det)))