saving capability
[imago.git] / imago.py
index 13702b0..f0d64e8 100755 (executable)
--- a/imago.py
+++ b/imago.py
@@ -11,6 +11,7 @@ imago.py --help
 """
 
 import sys
 """
 
 import sys
+import os
 import math
 
 try:
 import math
 
 try:
@@ -27,10 +28,14 @@ class UsageError(Exception):
     def __init__(self, msg):
         self.msg = msg
 
     def __init__(self, msg):
         self.msg = msg
 
+Saving_dir = ''
+Saving_num = 0
+
 def main(*argv):
     """Main function of the program."""
     
     show_all = False
 def main(*argv):
     """Main function of the program."""
     
     show_all = False
+    do_something = im_debug.show
 
     try:
         if argv is ():
 
     try:
         if argv is ():
@@ -42,6 +47,10 @@ def main(*argv):
             return 0
         if "--debug" in argv:
             show_all = True
             return 0
         if "--debug" in argv:
             show_all = True
+        if "--save" in argv:
+            global Saving_dir
+            Saving_dir = "saved/" + argv[0][:-4] + "/"
+            do_something = image_save
     except UsageError, err:
         print >>sys.stderr, err.msg, "(\"imago.py --help\" for help)"
         return 2
     except UsageError, err:
         print >>sys.stderr, err.msg, "(\"imago.py --help\" for help)"
         return 2
@@ -52,37 +61,37 @@ def main(*argv):
         print >>sys.stderr, msg
         return 1
     if show_all:
         print >>sys.stderr, msg
         return 1
     if show_all:
-        im_debug.show(image, "original image")
+        do_something(image, "original image")
 
     im_l = image.convert('L')
     if show_all:
 
     im_l = image.convert('L')
     if show_all:
-        im_debug.show(im_l, "ITU-R 601-2 luma transform")
+        do_something(im_l, "ITU-R 601-2 luma transform")
 
     im_edges = filters.edge_detection(im_l)
     if show_all:    
 
     im_edges = filters.edge_detection(im_l)
     if show_all:    
-        im_debug.show(im_edges, "edge detection")
+        do_something(im_edges, "edge detection")
 
     im_h = filters.high_pass(im_edges, 100)
     if show_all:
 
     im_h = filters.high_pass(im_edges, 100)
     if show_all:
-        im_debug.show(im_h, "high pass filters")
+        do_something(im_h, "high pass filters")
     
     hough1 = Hough(im_h.size)
     im_hough = hough1.transform(im_h)
     if show_all:
     
     hough1 = Hough(im_h.size)
     im_hough = hough1.transform(im_h)
     if show_all:
-        im_debug.show(im_hough, "hough transform")
+        do_something(im_hough, "hough transform")
 
     im_h2 = filters.high_pass(im_hough, 120)
     if show_all:
 
     im_h2 = filters.high_pass(im_hough, 120)
     if show_all:
-        im_debug.show(im_h2, "second high pass filters")
+        do_something(im_h2, "second high pass filters")
 
     hough2 = Hough(im_h2.size)
     im_hough2 = hough2.transform(im_h2)
     if show_all:
 
     hough2 = Hough(im_h2.size)
     im_hough2 = hough2.transform(im_h2)
     if show_all:
-        im_debug.show(im_hough2, "second hough transform")
+        do_something(im_hough2, "second hough transform")
 
     im_h3 = filters.high_pass(im_hough2, 120)
     if show_all:
 
     im_h3 = filters.high_pass(im_hough2, 120)
     if show_all:
-        im_debug.show(im_h3, "third high pass filters")
+        do_something(im_h3, "third high pass filters")
      
     lines = hough2.find_angle_distance(im_h3)
 
      
     lines = hough2.find_angle_distance(im_h3)
 
@@ -93,25 +102,34 @@ def main(*argv):
     for line in lines:
         draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
     if show_all:
     for line in lines:
         draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
     if show_all:
-        im_debug.show(im_lines, "lines")
+        do_something(im_lines, "lines")
 
     im_c = combine(im_h2, im_lines)
     if show_all:
 
     im_c = combine(im_h2, im_lines)
     if show_all:
-        im_debug.show(im_c, "first hough x lines")
+        do_something(im_c, "first hough x lines")
 
     collapse(im_c)
     if show_all:
 
     collapse(im_c)
     if show_all:
-        im_debug.show(im_c, "optimalised hough")
+        do_something(im_c, "optimalised hough")
 
     lines = hough1.all_lines(im_c)
     draw = ImageDraw.Draw(image)
     for line in lines:
         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
 
 
     lines = hough1.all_lines(im_c)
     draw = ImageDraw.Draw(image)
     for line in lines:
         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
 
-    im_debug.show(image, "the grid")
+    do_something(image, "the grid")
 
     return 0
 
 
     return 0
 
+def image_save(image, title=''):
+    global Saving_dir
+    global Saving_num
+    filename = Saving_dir + "{0:0>2}".format(Saving_num) + '.jpg'
+    if not os.path.isdir(Saving_dir):
+        os.makedirs(Saving_dir)
+    image.save(filename, 'JPEG')
+    Saving_num += 1
+
 def collapse(image):
     #HACK
     im_l = image.load()
 def collapse(image):
     #HACK
     im_l = image.load()