Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- import sys
- import os
- import Image
- image = sys.argv[1]
- todir = sys.argv[2].rstrip(os.sep)
- width = int(sys.argv[3])
- height = int(sys.argv[4])
- if len(sys.argv) > 5:
- vert = sys.argv[5].lower() in ("v", "vertical", "vert")
- else:
- vert = False
- if vert:
- def sizeIter(xcount, ycount):
- for x in range(xcount):
- for y in range(ycount):
- yield (x, y)
- else:
- def sizeIter(xcount, ycount):
- for y in range(ycount):
- for x in range(xcount):
- yield (x, y)
- if not os.path.isdir(todir):
- os.mkdir(todir)
- else:
- for file in os.listdir(todir):
- i = todir + os.sep + file
- if os.path.isfile(i): os.remove(i)
- imageBase = Image.open(image)
- imageX, imageY = imageBase.size
- xcount = imageX // width
- ycount = imageY // height
- i = 0
- for x, y in sizeIter(xcount, ycount):
- out = "{}{}image{:0>{}}.png".format(todir, os.sep, i, len(str(xcount * ycount)))
- box = [x * width, y * height, (x+1) * width, (y+1) * height]
- new = imageBase.crop(box)
- new.save(out)
- print("saved {}".format(out))
- i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement