Skip to content

Instantly share code, notes, and snippets.

@victorfei
Forked from glamp/get_digits.py
Created November 4, 2016 21:41
Show Gist options
  • Save victorfei/230b8c61b4b7ca7e7ccb4bbb92896c63 to your computer and use it in GitHub Desktop.
Save victorfei/230b8c61b4b7ca7e7ccb4bbb92896c63 to your computer and use it in GitHub Desktop.
import os
from PIL import Image
import numpy as np
files = [f for f in os.listdir("handwriting/numbers/")]
files = ["handwriting/numbers/" + f for f in files]
STANDARD_SIZE = (50, 50)
def get_image_data(filename):
img = Image.open(filename)
img = img.getdata()
img = img.resize(STANDARD_SIZE)
img = map(list, img)
img = np.array(img)
s = img.shape[0] * img.shape[1]
img_wide = img.reshape(1, s)
return img_wide[0]
data = []
labels = []
print "extracting features..."
for f in files:
data.append(get_image_data(f))
labels.append(int(f.split(".")[0][-1]))
print "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment