Skip to content

Instantly share code, notes, and snippets.

@sters
Created January 11, 2017 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sters/73a52f1a75b85f3958e293633ba31acf to your computer and use it in GitHub Desktop.
Save sters/73a52f1a75b85f3958e293633ba31acf to your computer and use it in GitHub Desktop.
# coding: utf-8
# In[14]:
get_ipython().magic(u'matplotlib inline')
from PIL import Image
import os
import sys
from matplotlib import pyplot as plt
# In[20]:
def splitter(im):
width, height = im.size
for x in range(0, width / 32):
for y in range(0, width / 32):
x1 = x * 32
y1 = y * 32
x2 = x1 + 32
y2 = y1 + 32
if x2 > width or y2 > height:
next
cropped = im.crop((int(x1), int(y1), int(x2), int(y2)))
yield cropped
# In[24]:
files = (
'1_before.jpg', '1_after.jpg',
'2_before.jpg', '2_after.jpg',
'3_before.jpg', '3_after.jpg',
)
for file in files:
im = Image.open(file)
name = file.replace('.jpg', '')
num = 1
for cropped in splitter(im):
cropped.save('images/' + file + "_%d.jpg"%num, "JPEG")
num += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment