Skip to content

Instantly share code, notes, and snippets.

@calston
Created September 26, 2012 14:30
Show Gist options
  • Save calston/3788377 to your computer and use it in GitHub Desktop.
Save calston/3788377 to your computer and use it in GitHub Desktop.
readImageStack
from PIL import Image, ImageOps
from matplotlib import pyplot
import numpy
def readImageStack(filename, w, h):
im = ImageOps.invert(Image.open(filename)).convert('L')
x, y = im.size
blY = y/h
blX = x/w
# Create blocks
blocks = []
for i in range(h):
for j in range(w):
x, y = (j*blX, i*blY)
# Get a block from the sample and resize to 32x32
imBlock = im.crop(
(x, y, x+blX, y+blY)).resize((32, 32))
blocks.append(
numpy.asarray(imBlock.convert('L')).flatten()
)
return numpy.vstack(blocks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment