Advertisement
TakesxiSximada

Untitled

Apr 11th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import argparse
  6. import numpy as np
  7. import matplotlib as plt
  8.  
  9. from PIL import (
  10.     Image,
  11.     ImageDraw,
  12.     )
  13.  
  14.  
  15. def get_window_locs(img_arr, hwcoef, scoef):
  16.     """
  17.    """
  18.     x, y = 0, 0
  19.     lis = []
  20.     # (h, w) = img_arr.shape
  21.     (h, w) = img_arr.size
  22.     hws = min(h, w)/hwcoef
  23.     s = hws/scoef
  24.     while y+hws <= h:
  25.         while x+hws <= w:
  26.             if x == 0 and y == 0:
  27.                 window_locs = np.array([x, y, x+hws, y+hws])
  28.             else:
  29.                 window_locs = np.vstack((window_locs, [x, y, x+hws, y+hws]))
  30.             x += s
  31.         x = 0
  32.         y += s
  33.     if window_locs.ndim == 1:
  34.         window_locs = window_locs[np.newaxis,:]
  35.     return window_locs
  36.  
  37.  
  38. def main(argv=sys.argv[1:]):
  39.     parser = argparse.ArgumentParser()
  40.     parser.add_argument('image')
  41.     args = parser.parse_args(argv)
  42.     im = Image.open(args.image)
  43.     height, width = im.size
  44.     img_size = 100
  45.     os.makedirs('trash', exist_ok=True)
  46.     for yy in range(0, height-img_size):
  47.         for xx in range(0, width-img_size):
  48.             box = (yy, xx, yy+img_size, xx+img_size)
  49.             image = im.crop(box)
  50.             image.save('trash/{}-{}-{}-{}.jpg'.format(box[0], box[1], box[2], box[3]))
  51.  
  52.     # img = ImageDraw.Draw(im)
  53.     res = get_window_locs(im, 1, 1)
  54.     print(args.image)
  55.  
  56. if __name__ == '__main__':
  57.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement