4 import Image, ImageDraw
11 def PixelGood(pixel, threshold):
13 return GreyScale(pixel) < threshold;
14 #return (pixel[1] > (pixel[0] + pixel[2]) and pixel[1] > threshold)
17 return (e[3] - e[1]) * (e[2] - e[0])
21 for i in range(0, len(pixel)):
23 greyscale = greyscale / len(pixel)
26 def SubDivide(pix, bounds, fill_fraction=0.1, threshold=200, seperation=4, min_area=250):
35 for x in range(bounds[0], bounds[2]):
36 for y in range(bounds[1], bounds[3]):
37 if PixelGood(pix[x, y], threshold):
38 if (x - xLast) > seperation:
39 if ((xEnd - xStart) * (yEnd - yStart) > min_area):
40 rect.append([xStart, yStart, xEnd, yEnd])
59 rect.append([xStart, yStart, xEnd, yEnd])
63 def GoodFraction(pix, bounds,threshold=200):
65 for x in range(int(bounds[0]), int(bounds[2])):
66 for y in range(int(bounds[1]), int(bounds[3])):
67 if PixelGood(pix[x, y], threshold):
70 return float(good) / float(int(bounds[2] - bounds[0])*int(bounds[3] - bounds[1]))
72 def PickDigit(pix, r):
74 # top, mid, bot, ltop, lbot, rtop, rmid
97 top = GoodFraction(pix,(r[0]+0.0*w, r[1], r[0]+1.0*w, r[1]+0.15*h)) # Top
98 mid = GoodFraction(pix,(r[0]+0.0*w, r[1]+0.45*h, r[0]+1.0*w, r[1]+0.55*h)) # Middle
99 bot = GoodFraction(pix,(r[0]+0.0*w, r[1]+0.9*h, r[0]+1.0*w, r[1]+h)) # Bottom
100 ltop = GoodFraction(pix,(r[0], r[1]+0.1*h, r[0]+0.4*w, r[1]+0.4*h)) # Left Top
101 lbot = GoodFraction(pix,(r[0], r[1]+0.6*h, r[0]+0.4*w, r[1]+0.9*h)) #Left Bottom
102 rtop = GoodFraction(pix,(r[0]+0.6*w, r[1]+0.1*h,r[0]+w, r[1]+0.4*h)) # Right Top
103 rbot = GoodFraction(pix,(r[0]+0.6*w, r[1]+0.6*h, r[0]+w, r[1]+0.9*h)) # Right Bottom
108 for f in [top,mid,bot,ltop,lbot,rtop,rbot]:
117 #Check where the boxes are
119 if (filled in d) == False:
120 img = Image.new("RGB", (r[2]-r[0], r[3]-r[1]), "white")
121 draw = ImageDraw.Draw(img)
122 for x in range(r[0], r[2]):
123 for y in range(r[1], r[3]):
124 draw.rectangle((x-r[0], y-r[1], x-r[0], y-r[1]), fill=pix[x, y], outline=None)
127 boxes.append((0, 0, w, 0.15*h)) # Top
128 boxes.append((0, 0.45*h, w, 0.55*h)) # Middle
129 boxes.append((0, 0.9*h, w, h)) # Bottom
130 boxes.append((0, 0, 0.4*w, 0.5*h)) # Left Top
131 boxes.append((0, 0.5*h, 0.4*w, h)) # Left Bottom
132 boxes.append((0.6*w, 0, w, 0.5*h)) # Right Top
133 boxes.append((0.6*w, 0.5*h, w, h)) # Right Bottom
135 draw.rectangle(box, fill=None, outline="blue")
144 def Process(fileName):
147 image = Image.open(fileName)
149 rect = SubDivide(pix, (0, 0, image.size[0], image.size[1]))
150 #print "Found " + str(len(rect)) + " rectangles"
151 #test = Image.new("RGB", image.size, "white")
153 #draw1 = ImageDraw.Draw(test)
154 #for i in range(0, len(rect)):
155 # draw1.rectangle(rect[i], fill=None, outline="red")
158 #img = Image.new("RGB", (rect[i][2]-rect[i][0], rect[i][3]-rect[i][1]), "white")
159 #draw2 = ImageDraw.Draw(img)
160 #for x in range(rect[i][0], rect[i][2]):
161 # for y in range(rect[i][1], rect[i][3]):
162 # draw2.rectangle((x-rect[i][0], y-rect[i][1], x-rect[i][0], y-rect[i][1]), fill=pix[x, y], outline=None)
168 digit = PickDigit(pix, r)
170 results.append(digit)
175 def PixToList(pix, bounds):
177 for x in range(bounds[0], bounds[2]):
180 for x in range(bounds[0], bounds[2]):
181 for y in range(bounds[1], bounds[3]):
183 a[x - bounds[0]].append(GreyScale(pix[x, y]))
184 #print(str(x) + "," + str(y), len(fft[x - bounds[0]]))
185 #print(str(fft[x - bounds[0]][y - bounds[1]]))
189 if __name__ == "__main__":
191 if (len(sys.argv) != 2):
192 print("Usage " + str(sys.argv[0]) + " input_image")
198 r = Process(sys.argv[1])
204 print(str(r[0]) + "." + str(r[1]) + str(r[2]) + "e-" + str(r[3]))
206 print(str(r[0]) + "." + str(r[1]) + "e-" + str(r[2]))