ba36b6d0bfe8e5dee6cf7d59e7057162ccc9ee3f
[imago.git] / intrsc.py
1 """Imago intersections module"""
2
3 from math import cos, tan, pi
4 from operator import itemgetter
5
6 import ImageDraw
7
8 def dst(line):
9     """Return normalized line."""
10     if line[0] < pi / 2:
11         line = line[0] + pi, - line[1]
12     return line
13
14 def dst_sort(lines):
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))
21     return lines
22
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)
27
28     if show_all:
29         image_g = image.copy()
30         draw = ImageDraw.Draw(image_g)
31         for line in intersections:
32             for (x, y) in line:
33                 draw.point((x , y), fill=(120, 255, 120))
34         do_something(image_g, "intersections")
35
36     board_r = []
37     board_raw = []
38     
39     for line in intersections:
40         board_r.append([stone_color(image, intersection) for intersection in
41                       line])
42         board_raw.append([stone_color_raw(image, intersection) for intersection in
43                       line])
44     return board_r, board_raw
45
46 def intersections_from_angl_dist(lines, size, get_all=True):
47     """Take grid-lines and size of the image. Return intersections."""
48     intersections = []
49     for (angl1, dist1) in lines[1]:
50         line = []
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)
61     return intersections
62    
63 def stone_color(image, (x, y)):
64     """Given image and coordinates, return stone color."""
65     suma = 0.
66     for i in range(-2, 3):
67         for j in range(-2, 3):
68             try:
69                 suma += sum(image.getpixel((x + i, y + j)))
70             except IndexError:
71                 pass
72     suma /= 3 * 25
73     if suma < 55:
74         return 'B'
75     elif suma < 200: 
76         return '.'
77     else:
78         return 'W'
79    
80 def stone_color_raw(image, (x, y)):
81     """Given image and coordinates, return stone color."""
82     suma = []
83     for i in range(-2, 3):
84         for j in range(-2, 3):
85             try:
86                 suma.append(image.getpixel((x + i, y + j)))
87             except IndexError:
88                 pass
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.)
91     return suma