From a0ffea12425b22e87010c097a393e9b627eb0a29 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Mon, 24 Sep 2012 14:52:19 +0200 Subject: [PATCH 1/1] imsave class --- imago.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/imago.py b/imago.py index ea9ee6c..810aa53 100755 --- a/imago.py +++ b/imago.py @@ -17,9 +17,6 @@ except ImportError, msg: import im_debug import linef -Saving_dir = '' -Saving_num = 0 - def main(): """Main function of the program.""" @@ -30,15 +27,13 @@ def main(): help="scale image to the specified width before analysis") parser.add_argument('-d', '--debug', dest='show_all', action='store_true', help="show every step of the computation") - parser.add_argument('-s', '--save', dest='do_something', action='store_const', - const=image_save, default=im_debug.show, + parser.add_argument('-s', '--save', dest='saving', action='store_true', help="save images instead of displaying them") parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help="report progress") args = parser.parse_args() show_all = args.show_all - do_something = args.do_something verbose = args.verbose try: @@ -52,8 +47,10 @@ def main(): if image.size[0] > args.w: image = image.resize((args.w, int((float(args.w)/image.size[0]) * image.size[1])), Image.ANTIALIAS) - global Saving_dir - Saving_dir = "saved/" + args.file[0][:-4] + "_" + str(image.size[0]) + "/" + do_something = im_debug.show + 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) @@ -89,14 +86,17 @@ def stone_color(image, (x, y)): else: return 'W' -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 +class imsave(): + def __init__(self, saving_dir): + self.saving_dir = saving_dir + self.saving_num = 0 + + def save(self, image, title=''): + filename = self.saving_dir + "{0:0>2}".format(self.saving_num) + '.jpg' + if not os.path.isdir(self.saving_dir): + os.makedirs(self.saving_dir) + image.save(filename, 'JPEG') + self.saving_num += 1 def combine(image1, image2): im_l1 = image1.load() -- 2.4.2