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)
30 hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
36 for event in pygame.event.get():
37 if event.type == pygame.QUIT:
40 if event.type == pygame.MOUSEBUTTONDOWN:
44 draw = ImageDraw.Draw(im)
47 corners.append(pygame.mouse.get_pos())
48 draw.point(corners[:-1], fill=color)
50 draw.line((corners[0], corners[1]), fill=color,
52 draw.line((corners[1], corners[2]), fill=color,
54 draw.line((corners[2], corners[3]), fill=color,
56 draw.line((corners[3], corners[0]), fill=color,
58 l_vert = lines(corners, 0)
60 draw.line(l, fill=color, width=line_width)
61 l_hor = lines(corners[1:4] + [corners[0]], 0)
63 draw.line(l, fill=color, width=line_width)
64 l_vert = sorted(l_vert)
68 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
69 line(l_hor[j][0], l_hor[j][1])))
71 screen.display_picture(im)
74 def lines(corners, n):
76 x = half_line(corners)
77 return (lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] +
78 lines([x[0], corners[1], corners[2], x[1]], n + 1))
80 x = half_line(corners)
81 c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
82 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
83 l = (intersection(line(corners[0], corners[1]), line(c,d)),
84 intersection(line(corners[2], corners[3]), line(c,d)))
85 l2 = half_line([corners[0], l[0], l[1], corners[3]])
87 return ([l, l2] + lines([l[0], l2[0], l2[1], l[1]], 2)
88 + lines([corners[0], l2[0], l2[1], corners[3]], 2)
89 + lines([l[0], corners[1], corners[2], l[1]], 2))
94 def half_line(corners):
96 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
100 l = line(c, (c[0] + corners[0][0] - corners[3][0],
101 c[1] + corners[0][1] - corners[3][1]))
102 p1 = intersection(l, line(corners[0], corners[1]))
103 p2 = intersection(l, line(corners[2], corners[3]))
108 return intersection(line(corners[0], corners[2]),
109 line(corners[1], corners[3]))
113 c = a * y[0] + b * y[1]
116 def intersection(p, q):
117 det = p[0] * q[1] - p[1] * q[0]
120 return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)),
121 int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))