manual mode user interface
authorTomas Musil <tomik.musil@gmail.com>
Fri, 5 Oct 2012 20:52:34 +0000 (22:52 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Fri, 5 Oct 2012 20:52:34 +0000 (22:52 +0200)
imago.py
manual.py [new file with mode: 0644]

index 810aa53..830a53b 100755 (executable)
--- a/imago.py
+++ b/imago.py
@@ -16,6 +16,7 @@ except ImportError, msg:
 
 import im_debug
 import linef
 
 import im_debug
 import linef
+import manual
 
 def main():
     """Main function of the program."""
 
 def main():
     """Main function of the program."""
@@ -25,6 +26,8 @@ def main():
                         help="image to analyse")
     parser.add_argument('-w', type=int, default=640,
                         help="scale image to the specified width before analysis")
                         help="image to analyse")
     parser.add_argument('-w', type=int, default=640,
                         help="scale image to the specified width before analysis")
+    parser.add_argument('-m', '--manual', dest='manual_mode', action='store_true',
+                        help="manual grid selection")
     parser.add_argument('-d', '--debug', dest='show_all', action='store_true',
                         help="show every step of the computation")
     parser.add_argument('-s', '--save', dest='saving', action='store_true',
     parser.add_argument('-d', '--debug', dest='show_all', action='store_true',
                         help="show every step of the computation")
     parser.add_argument('-s', '--save', dest='saving', action='store_true',
@@ -51,8 +54,15 @@ def main():
     if args.saving:
         do_something = imsave("saved/" + args.file[0][:-4] + "_" +
                                str(image.size[0]) + "/").save
     if args.saving:
         do_something = imsave("saved/" + args.file[0][:-4] + "_" +
                                str(image.size[0]) + "/").save
-    
-    lines = linef.find_lines(image, show_all, do_something, verbose)
+
+    if args.manual_mode:
+        try:
+            lines = manual.find_lines(image)
+        except manual.UserQuitError:
+            #TODO ask user to try again
+            return 1
+    else:
+        lines = linef.find_lines(image, show_all, do_something, verbose)
 
     intersections = intersections_from_angl_dist(lines, image.size)
     image_g = image.copy()
 
     intersections = intersections_from_angl_dist(lines, image.size)
     image_g = image.copy()
diff --git a/manual.py b/manual.py
new file mode 100644 (file)
index 0000000..a2c4f50
--- /dev/null
+++ b/manual.py
@@ -0,0 +1,57 @@
+"""Manual grid selection module"""
+
+import pygame
+import Image, ImageDraw
+
+class UserQuitError(Exception):
+    pass
+
+class Screen:
+    def __init__(self, res):
+        pygame.init()
+        pygame.display.set_mode(res)
+        pygame.display.set_caption("Go image capture")
+        self._screen = pygame.display.get_surface()
+
+    def display_picture(self, im):
+        pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
+        self._screen.blit(pg_img, (0,0))
+        pygame.display.flip()
+
+def find_lines(im_orig):
+
+    im = im_orig.copy()
+
+    screen = Screen(im.size)
+
+    done = False
+    clock = pygame.time.Clock()
+    draw = ImageDraw.Draw(im)
+    corners = []
+    color=(64, 64, 255)
+    line_width = 2
+    while not done:
+        for event in pygame.event.get():
+            if event.type == pygame.QUIT:
+                pygame.quit()
+                raise UserQuitError 
+            if event.type == pygame.MOUSEBUTTONDOWN:
+               if len(corners) >= 4:
+                    corners = []
+                    im = im_orig.copy()
+                    draw = ImageDraw.Draw(im)
+
+               if len(corners) < 4:
+                    corners.append(pygame.mouse.get_pos())
+                    draw.point(corners[:-1], fill=color)
+                    if len(corners) == 4:
+                        draw.line((corners[0], corners[1]), fill=color,
+                                  width=line_width)
+                        draw.line((corners[1], corners[2]), fill=color,
+                                  width=line_width)
+                        draw.line((corners[2], corners[3]), fill=color,
+                                  width=line_width)
+                        draw.line((corners[3], corners[0]), fill=color,
+                                  width=line_width)
+        screen.display_picture(im)
+        clock.tick(15)