color distribution plot
[imago.git] / manual.py
1 """Manual grid selection module"""
2
3 import pygame
4 import ImageDraw
5 from math import sqrt, acos, copysign
6 from geometry import l2ad, line, intersection
7
8 class UserQuitError(Exception):
9     pass
10
11 class Screen:
12     def __init__(self, res):
13         pygame.init()
14         pygame.display.set_mode(res)
15         pygame.display.set_caption("Imago manual mode")
16         self._screen = pygame.display.get_surface()
17
18     def display_picture(self, img):
19         pg_img = pygame.image.frombuffer(img.tostring(), img.size, img.mode)
20         self._screen.blit(pg_img, (0,0))
21         pygame.display.flip()
22
23 def find_lines(im_orig):
24
25     im = im_orig.copy()
26
27     screen = Screen(im.size)
28
29     done = False
30     clock = pygame.time.Clock()
31     draw = ImageDraw.Draw(im)
32     hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
33                  fill=(255, 64, 64))
34     corners = []
35     color = (64, 64, 255)
36     line_width = 1
37     lines_r = []
38
39     while not done:
40         for event in pygame.event.get():
41             if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
42                 pygame.quit()
43                 if len(corners) == 4:
44                     return lines_r
45                 else:
46                     raise UserQuitError 
47             if event.type == pygame.MOUSEBUTTONDOWN:
48                 if len(corners) >= 4: 
49                     corners = []
50                     im = im_orig.copy()
51                     draw = ImageDraw.Draw(im)
52
53                 if len(corners) < 4:
54                     corners.append(pygame.mouse.get_pos())
55                     draw.point(corners[:-1], fill=color)
56                     if len(corners) == 4:
57                         l_vert, l_hor = lines(corners)
58                         for l in l_vert:
59                             draw.line(l, fill=color, width=line_width)
60                         for l in l_hor:
61                             draw.line(l, fill=color, width=line_width)
62                         #TODO sort by distance
63                         l_vert.sort()
64                         l_hor.sort()
65                         for i in [3, 9, 15]:
66                             for j in [3, 9, 15]:
67                                 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
68                                                    line(l_hor[j][0], l_hor[j][1])))
69                         lines_r = [[l2ad(l, im.size) for l in l_vert], 
70                                    [l2ad(l, im.size) for l in l_hor]]
71
72         screen.display_picture(im)
73         clock.tick(15)
74
75 def lines(corners):
76     #TODO Error on triangle 
77     cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
78              corners[1:]]
79     cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) *
80               sqrt(b[0] **2 + b[1] ** 2)), a[0] * b[1] - b[0] * a[1], c) for a, b, c in cor_d]
81     cor_d = sorted([(copysign(acos(min(a, 1)), b), c) for a, b, c in cor_d])
82     corners = [corners[0]] + [c for _, c in cor_d]
83     return (_lines(corners, 0) + [(corners[0], corners[3]),
84                                   (corners[1], corners[2])],
85             _lines(corners[1:4] + [corners[0]], 0) + 
86             [(corners[0], corners[1]), (corners[2], corners[3])])
87
88 def _lines(corners, n):
89     if n == 0:
90         x = half_line(corners)
91         return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] + 
92                 _lines([x[0], corners[1], corners[2], x[1]], n + 1))
93     else:
94         x = half_line(corners)
95         c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
96         d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
97         if d:
98             l = (intersection(line(corners[0], corners[1]), line(c, d)),
99                  intersection(line(corners[2], corners[3]), line(c, d)))
100         else:
101             lx = line(c, (c[0] + corners[0][0] - corners[3][0], 
102                       c[1] + corners[0][1] - corners[3][1]))
103             l = (intersection(line(corners[0], corners[1]), lx),
104                  intersection(line(corners[2], corners[3]), lx))
105         l2 = half_line([corners[0], l[0], l[1], corners[3]])
106         if n == 1:
107             return ([l, l2] + _lines([l[0], l2[0], l2[1], l[1]], 2)
108                     + _lines([corners[0], l2[0], l2[1], corners[3]], 2)
109                     + _lines([l[0], corners[1], corners[2], l[1]], 2))
110         if n == 2:
111             return [l, l2]
112
113
114 def half_line(corners):
115     c = center(corners)
116     d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
117     if d:
118         l = line(c, d)
119     else:
120         l = line(c, (c[0] + corners[0][0] - corners[3][0], 
121                      c[1] + corners[0][1] - corners[3][1]))
122     p1 = intersection(l, line(corners[0], corners[1]))
123     p2 = intersection(l, line(corners[2], corners[3]))
124     return (p1, p2)
125
126
127 def center(corners):
128     return intersection(line(corners[0], corners[2]), 
129                         line(corners[1], corners[3]))