# use PIL to count pixels in letters I and O
# image file needs to be a bitmap file to maintain
# red (255,0,0) and black (0,0,0) pixel colors correctly
from PIL import Image
# img_I.bmp --> 50x50 black bg + red I (times new roman, bold, 24)
img_I = Image.open("img_I.bmp").getdata()
# img_O.bmp --> 50x50 black bg + red O (times new roman, bold, 24)
img_O = Image.open("img_O.bmp").getdata()
# count red pixels of the I
# list(img_I) is a list of (r,g,b) color tuples for each pixel
# where (255,0,0) would be the red pixel
red_I = 0
for red in list(img_I):
if red == (255,0,0):
red_I += 1
print red_I # 76
# count red pixels of the O
red_O = 0
for red in list(img_O):
if red == (255,0,0):
red_O += 1
print red_O # 126