Commits

Sven Hendriks committed 067e336

Added output path and output file related sanity checks.

  • Participants
  • Parent commits c732473

Comments (0)

Files changed (1)

File panslice/panslice.py

 
     for i, x in enumerate(range(-x_offset, im.size[0], slice_width)):
         tile = im.crop((x, -y_offset, x + slice_width, im.size[1]+y_offset))
-        tile.save(os.path.join(output_path, 
-            '%s_tile_%03d.jpg' % (input_filename, (i+1))), quality=95)
+        tilefilename = os.path.join(output_path, 
+            '%s_tile_%03d.jpg' % (input_filename, (i+1)))
+        if os.path.exists(tilefilename):
+            raise RuntimeError('File %s already exists.' % (tilefilename,))
+        tile.save(tilefilename, quality=95)
         sys.stdout.write('.')
         sys.stdout.flush()
-    print
+    print ' Done.'
 
 
 def cli():
     try:
         ratio = [float(x) for x in opts.ratio.split('x')]
     except ValueError:
-        sys.exit('Invalid ratio format. Cannot convert to float.')
+        sys.exit('Error: Invalid ratio format. Cannot convert to float.')
     else:
         if len(ratio) != 2:
-            sys.exit('Invalid ratio format. Need exactly two numbers ' \
+            sys.exit('Error: Invalid ratio format. Need exactly two numbers ' \
                 'separated by an "x"')
 
+    if not os.path.exists(opts.output_path):
+        sys.exit('Error: Path %s does not exist.' % (opts.output_path,))
+
     if len(args) not in (1, 2):
         sys.exit('Usage: %s [options] <input file> [<slice width>]' 
             % (sys.argv[0],))
             slice_image(args[0], x_center=opts.x_center, y_center=opts.y_center,
                         ratio=ratio, output_path=opts.output_path)
 
-