-
-
Save vaslabs/8345933 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/python | |
from operator import itemgetter | |
from PIL import Image, ImageDraw, ImageFont | |
# Make a lowercase + uppercase alphabet. | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
alphabet += ''.join(map(str.upper, alphabet)) | |
#alphabet="z" | |
# We'll use Helvetica in big type. | |
helvetica = ImageFont.truetype('/usr/share/fonts/liberation/LiberationSerif-Regular.ttf', 100) | |
def draw_letter(letter, save=True): | |
img = Image.new('RGB', (100, 100), 'white') | |
draw = ImageDraw.Draw(img) | |
draw.text((0,0), letter, font=helvetica, fill='#000000') | |
if save: | |
img.save("imgs/{}.png".format(letter), 'PNG') | |
img = img.rotate(90) #hack to find width | |
return img | |
def count_black_pixels(img): | |
pixels = list(img.getdata()) | |
return len(filter(lambda rgb: sum(rgb) == 0, pixels)) | |
def convert2D(data, size): | |
a = [] | |
for i in range(0, size): | |
b = [] | |
for j in range(0, size): | |
b.append(data[i*size+j]); | |
a.append(b) | |
return a | |
def count_width(img): | |
pixels = convert2D(img.getdata(), 100) | |
width_sum = 0 | |
blank_width = 0 | |
turn_blank_counting_on = False | |
for i in pixels: | |
if len(filter(lambda rgb: sum(rgb) == 0, i)) > 0: | |
width_sum += 1 | |
width_sum += blank_width | |
blank_width = 0 | |
turn_blank_counting_on = True | |
elif turn_blank_counting_on: | |
blank_width += 1 | |
return width_sum | |
def count_height(img): | |
img = img.rotate(90) | |
return count_width(img) | |
if __name__ == '__main__': | |
stats = [] | |
for letter in alphabet: | |
img = draw_letter(letter) | |
width = count_width(img) | |
height = count_height(img) | |
area = width*height | |
stats.append((letter, width, height, area)) | |
print "Descending order : pixels occupy" | |
print "---------------------------------" | |
print sorted(stats, key=itemgetter(3), reverse=True) | |
print "Descending order : width" | |
print "---------------------------------" | |
print sorted(stats, key=itemgetter(1), reverse=True) | |
print "Descending order : height" | |
print "---------------------------------" | |
print sorted(stats, key=itemgetter(2), reverse=True) |
This file contains hidden or 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
Descending order : pixels occupy | |
--------------------------------- | |
[('W', 93, 66, 6138), ('M', 83, 65, 5395), ('Q', 62, 76, 4712), ('A', 70, 66, 4620), ('V', 69, 66, 4554), ('K', 68, 65, 4420), ('X', 67, 65, 4355), ('Y', 67, 65, 4355), ('H', 66, 65, 4290), ('N', 66, 65, 4290), ('U', 66, 65, 4290), ('D', 64, 65, 4160), ('G', 63, 65, 4095), ('R', 63, 65, 4095), ('O', 62, 65, 4030), ('T', 58, 65, 3770), ('B', 57, 65, 3705), ('C', 55, 65, 3575), ('E', 52, 65, 3380), ('m', 73, 46, 3358), ('w', 70, 47, 3290), ('L', 49, 65, 3185), ('Z', 49, 65, 3185), ('h', 46, 69, 3174), ('k', 46, 69, 3174), ('F', 48, 65, 3120), ('P', 48, 65, 3120), ('b', 44, 69, 3036), ('d', 43, 69, 2967), ('S', 42, 65, 2730), ('y', 44, 57, 2508), ('p', 43, 57, 2451), ('g', 40, 61, 2440), ('v', 49, 47, 2303), ('J', 35, 65, 2275), ('x', 48, 46, 2208), ('q', 38, 58, 2204), ('n', 45, 46, 2070), ('u', 45, 46, 2070), ('f', 29, 69, 2001), ('o', 40, 46, 1840), ('z', 38, 46, 1748), ('a', 37, 46, 1702), ('c', 36, 46, 1656), ('e', 36, 46, 1656), ('I', 25, 65, 1625), ('l', 22, 69, 1518), ('t', 26, 56, 1456), ('i', 22, 65, 1430), ('r', 30, 47, 1410), ('s', 30, 46, 1380), ('j', 14, 76, 1064)] | |
Descending order : width | |
--------------------------------- | |
[('W', 93, 66, 6138), ('M', 83, 65, 5395), ('m', 73, 46, 3358), ('w', 70, 47, 3290), ('A', 70, 66, 4620), ('V', 69, 66, 4554), ('K', 68, 65, 4420), ('X', 67, 65, 4355), ('Y', 67, 65, 4355), ('H', 66, 65, 4290), ('N', 66, 65, 4290), ('U', 66, 65, 4290), ('D', 64, 65, 4160), ('G', 63, 65, 4095), ('R', 63, 65, 4095), ('O', 62, 65, 4030), ('Q', 62, 76, 4712), ('T', 58, 65, 3770), ('B', 57, 65, 3705), ('C', 55, 65, 3575), ('E', 52, 65, 3380), ('v', 49, 47, 2303), ('L', 49, 65, 3185), ('Z', 49, 65, 3185), ('x', 48, 46, 2208), ('F', 48, 65, 3120), ('P', 48, 65, 3120), ('h', 46, 69, 3174), ('k', 46, 69, 3174), ('n', 45, 46, 2070), ('u', 45, 46, 2070), ('b', 44, 69, 3036), ('y', 44, 57, 2508), ('d', 43, 69, 2967), ('p', 43, 57, 2451), ('S', 42, 65, 2730), ('g', 40, 61, 2440), ('o', 40, 46, 1840), ('q', 38, 58, 2204), ('z', 38, 46, 1748), ('a', 37, 46, 1702), ('c', 36, 46, 1656), ('e', 36, 46, 1656), ('J', 35, 65, 2275), ('r', 30, 47, 1410), ('s', 30, 46, 1380), ('f', 29, 69, 2001), ('t', 26, 56, 1456), ('I', 25, 65, 1625), ('i', 22, 65, 1430), ('l', 22, 69, 1518), ('j', 14, 76, 1064)] | |
Descending order : height | |
--------------------------------- | |
[('j', 14, 76, 1064), ('Q', 62, 76, 4712), ('b', 44, 69, 3036), ('d', 43, 69, 2967), ('f', 29, 69, 2001), ('h', 46, 69, 3174), ('k', 46, 69, 3174), ('l', 22, 69, 1518), ('A', 70, 66, 4620), ('V', 69, 66, 4554), ('W', 93, 66, 6138), ('i', 22, 65, 1430), ('B', 57, 65, 3705), ('C', 55, 65, 3575), ('D', 64, 65, 4160), ('E', 52, 65, 3380), ('F', 48, 65, 3120), ('G', 63, 65, 4095), ('H', 66, 65, 4290), ('I', 25, 65, 1625), ('J', 35, 65, 2275), ('K', 68, 65, 4420), ('L', 49, 65, 3185), ('M', 83, 65, 5395), ('N', 66, 65, 4290), ('O', 62, 65, 4030), ('P', 48, 65, 3120), ('R', 63, 65, 4095), ('S', 42, 65, 2730), ('T', 58, 65, 3770), ('U', 66, 65, 4290), ('X', 67, 65, 4355), ('Y', 67, 65, 4355), ('Z', 49, 65, 3185), ('g', 40, 61, 2440), ('q', 38, 58, 2204), ('p', 43, 57, 2451), ('y', 44, 57, 2508), ('t', 26, 56, 1456), ('r', 30, 47, 1410), ('v', 49, 47, 2303), ('w', 70, 47, 3290), ('a', 37, 46, 1702), ('c', 36, 46, 1656), ('e', 36, 46, 1656), ('m', 73, 46, 3358), ('n', 45, 46, 2070), ('o', 40, 46, 1840), ('s', 30, 46, 1380), ('u', 45, 46, 2070), ('x', 48, 46, 2208), ('z', 38, 46, 1748)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment