Created
September 26, 2012 14:30
-
-
Save calston/3788377 to your computer and use it in GitHub Desktop.
readImageStack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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