Skip to content

Instantly share code, notes, and snippets.

@nakamura001
Created December 13, 2011 06:07
Show Gist options
  • Save nakamura001/1470859 to your computer and use it in GitHub Desktop.
Save nakamura001/1470859 to your computer and use it in GitHub Desktop.
PILで画像を切り取り
#! /usr/bin/env python
# coding: utf-8
import os,re,sys
import tempfile
import shutil
from PIL import Image
in_dir = "org_imgs"
out_dir = "crop_imgs"
image_size = 320
count = 0
for (root, dirs, files) in os.walk(in_dir):
for f in files:
d, ext = os.path.splitext(f)
if ext == '':
continue
in_file = os.path.join(in_dir, f)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
out_file = os.path.join(out_dir, d+'.png')
print out_file
im = Image.open(in_file)
w, h = im.size
x = (w-image_size)/2
y = (h-image_size)/2
box = (x, y, x+image_size, y+image_size)
im2 = im.crop(box)
im2.save(out_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment