1 """Manual grid selection module"""
4 import Image, ImageDraw
5 from math import atan, sin, cos, pi
7 class UserQuitError(Exception):
11 def __init__(self, res):
13 pygame.display.set_mode(res)
14 pygame.display.set_caption("Go image capture")
15 self._screen = pygame.display.get_surface()
17 def display_picture(self, im):
18 pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
19 self._screen.blit(pg_img, (0,0))
22 def find_lines(im_orig):
26 screen = Screen(im.size)
29 clock = pygame.time.Clock()
30 draw = ImageDraw.Draw(im)
31 hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
39 for event in pygame.event.get():
40 if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
46 if event.type == pygame.MOUSEBUTTONDOWN:
50 draw = ImageDraw.Draw(im)
53 corners.append(pygame.mouse.get_pos())
54 draw.point(corners[:-1], fill=color)
56 draw.line((corners[0], corners[1]), fill=color,
58 draw.line((corners[1], corners[2]), fill=color,
60 draw.line((corners[2], corners[3]), fill=color,
62 draw.line((corners[3], corners[0]), fill=color,
64 l_vert = lines(corners, 0)
66 draw.line(l, fill=color, width=line_width)
67 l_hor = lines(corners[1:4] + [corners[0]], 0)
69 draw.line(l, fill=color, width=line_width)
70 l_vert += [(corners[0], corners[3]),
71 (corners[1], corners[2])]
72 l_hor += [(corners[0], corners[1]),
73 (corners[2], corners[3])]
74 l_vert = sorted(l_vert)
78 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
79 line(l_hor[j][0], l_hor[j][1])))
80 lines_r = [[l2ad(l[0], l[1], im.size) for l in l_vert],
81 [l2ad(l[0], l[1], im.size) for l in l_hor]]
83 screen.display_picture(im)
86 def lines(corners, n):
88 x = half_line(corners)
89 return (lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] +
90 lines([x[0], corners[1], corners[2], x[1]], n + 1))
92 x = half_line(corners)
93 c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
94 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
95 l = (intersection(line(corners[0], corners[1]), line(c,d)),
96 intersection(line(corners[2], corners[3]), line(c,d)))
97 l2 = half_line([corners[0], l[0], l[1], corners[3]])
99 return ([l, l2] + lines([l[0], l2[0], l2[1], l[1]], 2)
100 + lines([corners[0], l2[0], l2[1], corners[3]], 2)
101 + lines([l[0], corners[1], corners[2], l[1]], 2))
106 def half_line(corners):
108 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
112 l = line(c, (c[0] + corners[0][0] - corners[3][0],
113 c[1] + corners[0][1] - corners[3][1]))
114 p1 = intersection(l, line(corners[0], corners[1]))
115 p2 = intersection(l, line(corners[2], corners[3]))
120 return intersection(line(corners[0], corners[2]),
121 line(corners[1], corners[3]))
125 c = a * y[0] + b * y[1]
128 def intersection(p, q):
129 det = p[0] * q[1] - p[1] * q[0]
132 return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)),
133 int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))
135 def l2ad(a, b, size):
136 if (a[0] - b[0]) == 0:
139 q = float(a[1] - b[1]) / (a[0] - b[0])
142 distance = (((a[0] - (size[0] / 2)) * sin(angle)) +
143 ((a[1] - (size[1] / 2)) * - cos(angle)))
144 return (angle, distance)