Skip to content

Instantly share code, notes, and snippets.

@TakesxiSximada
Created April 12, 2015 13:58
Show Gist options
  • Save TakesxiSximada/ca1b5aac871ec7167ff9 to your computer and use it in GitHub Desktop.
Save TakesxiSximada/ca1b5aac871ec7167ff9 to your computer and use it in GitHub Desktop.
画像をスライドさせ分割
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import argparse
import numpy as np
import matplotlib as plt
from PIL import (
Image,
ImageDraw,
)
def main(argv=sys.argv[1:]):
parser = argparse.ArgumentParser()
parser.add_argument('image')
parser.add_argument('-s', '--size', default=50, type=int)
parser.add_argument('-o', '--output', default='var/images')
args = parser.parse_args(argv)
im = Image.open(args.image)
height, width = im.size
img_size = args.size
img_size_half = int(img_size / 2)
basename = os.path.basename(args.image)
output_dir = os.path.join(args.output, basename)
os.makedirs(output_dir, exist_ok=True)
for yy in range(0, height-img_size, img_size_half):
for xx in range(0, width-img_size, img_size_half):
box = (yy, xx, yy+img_size, xx+img_size)
image = im.crop(box)
output_name = '{}-{}-{}-{}.jpg'.format(box[0], box[1], box[2], box[3])
output_path = os.path.join(output_dir, output_name)
image.save(output_path)
# img = ImageDraw.Draw(im)
print(args.image)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment