1 """Imago intersections module"""
3 from math import cos, tan, pi
4 from operator import itemgetter
9 """Return normalized line."""
11 line = line[0] + pi, - line[1]
15 """Return lines sorted by distance."""
16 l_max = max(l[0] for l in lines)
17 l_min = min(l[0] for l in lines)
18 if l_max - l_min > (3. / 4) * pi:
19 lines = [dst(l) for l in lines]
20 lines.sort(key=itemgetter(1))
23 def board(image, lines, show_all, do_something):
24 """Compute intersections, find stone colors and return board situation."""
25 lines = [dst_sort(l) for l in lines]
26 intersections = intersections_from_angl_dist(lines, image.size)
29 image_g = image.copy()
30 draw = ImageDraw.Draw(image_g)
31 for line in intersections:
33 draw.point((x , y), fill=(120, 255, 120))
34 do_something(image_g, "intersections")
39 for line in intersections:
40 board_r.append([stone_color(image, intersection) for intersection in
42 board_raw.append([stone_color_raw(image, intersection) for intersection in
44 return board_r, board_raw
46 def intersections_from_angl_dist(lines, size, get_all=True):
47 """Take grid-lines and size of the image. Return intersections."""
49 for (angl1, dist1) in lines[1]:
51 for (angl2, dist2) in lines[0]:
52 if abs(angl1 - angl2) > 0.4:
53 i_x = (- ((dist2 / cos(angl2)) - (dist1 / cos(angl1)))
54 / (tan(angl1) - tan(angl2)))
55 i_y = (tan(angl1) * i_x) - (dist1 / cos(angl1))
56 if get_all or (-size[0] / 2 < i_x < size[0] / 2 and
57 -size[1] / 2 < i_y < size[1] / 2):
58 line.append((int(i_x + size[0] / 2),
59 int(i_y + size[1] / 2)))
60 intersections.append(line)
63 def stone_color(image, (x, y)):
64 """Given image and coordinates, return stone color."""
66 for i in range(-2, 3):
67 for j in range(-2, 3):
69 suma += sum(image.getpixel((x + i, y + j)))
80 def stone_color_raw(image, (x, y)):
81 """Given image and coordinates, return stone color."""
83 for i in range(-2, 3):
84 for j in range(-2, 3):
86 suma.append(image.getpixel((x + i, y + j)))
89 suma = (sum(s[0] for s in suma) / 25., sum(s[1] for s in suma) / 25.,
90 sum(s[2] for s in suma) / 25.)