Skip to content

Instantly share code, notes, and snippets.

@shravan-shandilya
Created March 27, 2017 13:07
Show Gist options
  • Save shravan-shandilya/e7c6bc31b37bd276c0aca6160efe9b8a to your computer and use it in GitHub Desktop.
Save shravan-shandilya/e7c6bc31b37bd276c0aca6160efe9b8a to your computer and use it in GitHub Desktop.
bulk_downsizer_and_sampler.py
#!/usr/bin/python
from PIL import Image
import os
resize = (20,20)
read_path = "Img/Sample%03d/img%03d-%03d.png"
write_path = "Downscaled/Sample%03d/img%03d-%03d.png"
dataset = open("kannada.csv","w+")
for letter in range(1,18):
for sample in range(1,26):
#print(img_path%(letter,letter,sample))
image = Image.open(read_path%(letter,letter,sample))
image = image.resize(resize, Image.ANTIALIAS)
try:
os.stat("Downscaled/Sample%03d"%letter)
except:
os.mkdir("Downscaled/Sample%03d"%letter)
image.save(write_path%(letter,letter,sample),"PNG")
pixels = image.load()
line = ""
for i in range(0,20):
for j in range(0,20):
grayscale_value = 0.299*pixels[i,j][0] + 0.587*pixels[i,j][1] + 0.114*pixels[i,j][2]
line+=str(grayscale_value)+","
line+=str(letter)+"\n"
dataset.write(line)
print "Completed a sample"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment