Created
May 12, 2014 01:23
-
-
Save uttamg911/1d7c78a04ae95b1c1e30 to your computer and use it in GitHub Desktop.
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
# imports image module from the PIL libary | |
from PIL import Image | |
# opens the favicon as an object to read the pixel values | |
im = Image.open('Favicons/InNOut.png') | |
# alphabet string to make the keys of the dictionary | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
# gets the frequencies of all the pixel values as an RGB tuple and stores them as a list | |
pdata = list(im.getcolors()) | |
# a list which stores the normalized RGB values of the image into 27 buckets. | |
newdata = [] | |
# a dictionary which associates the 26 RGB buckets to the english alphabet | |
alphaDict = {(0,0,0):'a',(0,0,1):'b',(0,0,2):'c',(0,1,0):'d',(0,1,1):'e',(0,1,2):'f',(0,2,0):'g',(0,2,1):'h',(0,2,2):'i',(1,0,0):'j',(1,0,1):'k',(1,0,2):'l',(1,1,0):'m',(1,1,1):'n',(1,1,2):'o',(1,2,0):'p',(1,2,1):'q',(1,2,2):'r',(2,0,0):'s',(2,0,1):'t',(2,0,2):'u',(2,1,0):'v',(2,1,1):'w',(2,1,2):'x',(2,2,0):'y',(2,2,1):'z'} | |
# Contains the frequency of the 26 RGB buckets. | |
freqDict = {} | |
# Contains the alphabet counts as key-value pairs | |
# Final Output | |
countDict = {} | |
# pdata[0] - element | |
# pdata[0][0] - frequency | |
# pdata[0][0][r] - color value | |
# converts the RGB space (255 values in each channel) into the normalized space (3 values in each channel => 3x3x3 = 27 buckets) | |
for rzero in range(0, len(im.getcolors())-1): | |
#del qelement[:] #clear temporary list | |
qelement = [] | |
for rone in range(0,3): | |
if pdata[rzero][1][rone] <= 255 and pdata[rzero][1][rone] >169: | |
qelement.append(2) | |
elif pdata[rzero][1][rone] <= 169 and pdata[rzero][1][rone] >84: | |
qelement.append(1) | |
elif pdata[rzero][1][rone] <= 84: | |
qelement.append(0) | |
newdata.append(qelement) | |
# calculates the frequency of the 26 RGB buckets | |
for i in newdata: | |
if ( freqDict.has_key(tuple(i))): | |
freqDict[tuple(i)] += 1 | |
else: | |
freqDict[tuple(i)] = 1 | |
# constructs a key value pair of letters based on the frequency for each RGB bucket | |
for value in alphaDict: | |
countDict[alphaDict[value]] = freqDict[value] if freqDict.has_key(value) else 0 | |
# prints the output | |
print countDict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment