Advertisement
ijontichy

getimg.py

Jun 2nd, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. import sys
  4. import os
  5. import Image
  6.  
  7. image = sys.argv[1]
  8. todir = sys.argv[2].rstrip(os.sep)
  9. width = int(sys.argv[3])
  10. height = int(sys.argv[4])
  11.  
  12. if len(sys.argv) > 5:
  13.     vert = sys.argv[5].lower() in ("v", "vertical", "vert")
  14. else:
  15.     vert = False
  16.  
  17. if vert:
  18.     def sizeIter(xcount, ycount):
  19.         for x in range(xcount):
  20.             for y in range(ycount):
  21.                 yield (x, y)
  22.  
  23. else:
  24.     def sizeIter(xcount, ycount):
  25.         for y in range(ycount):
  26.             for x in range(xcount):
  27.                 yield (x, y)
  28.  
  29. if not os.path.isdir(todir):
  30.     os.mkdir(todir)
  31.  
  32. else:
  33.     for file in os.listdir(todir):
  34.         i = todir + os.sep + file
  35.         if os.path.isfile(i): os.remove(i)
  36.  
  37. imageBase = Image.open(image)
  38. imageX, imageY = imageBase.size
  39.  
  40. xcount = imageX // width
  41. ycount = imageY // height
  42. i = 0
  43.  
  44. for x, y in sizeIter(xcount, ycount):
  45.     out = "{}{}image{:0>{}}.png".format(todir, os.sep, i, len(str(xcount * ycount)))
  46.     box = [x * width, y * height, (x+1) * width, (y+1) * height]
  47.     new = imageBase.crop(box)
  48.     new.save(out)
  49.     print("saved {}".format(out))
  50.  
  51.     i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement