我希望得到這個圖片中的數字,使用Python和PIL在顯示的數目:使用Python PIL獲得的圖像
我的代碼:
#!/usr/bin/env python
#encoding=utf-8
import Image,ImageEnhance,ImageFilter
import Data
DEBUG = False
def d_print(*msg):
global DEBUG
if DEBUG:
for i in msg:
print(i),
print
else:
pass
def Get_Num(l=[]):
min1 = []
min2 = []
print(l)
for n in Data.N:
count1=count2=count3=count4=0
if (len(l) != len(n)):
print("Wrong pic %s %s " %(len(l),len(n)))
exit()
for i in range(len(l)):
if (l[i] == 1):
count1+=1
if (n[i] == 1):
count2+=1
for i in range(len(l)):
if (n[i] == 1):
count3+=1
if (l[i] == 1):
count4+=1
d_print(count1,count2,count3,count4)
min1.append(count1-count2)
min2.append(count3-count4)
d_print(min1,"\n",min2)
for i in range(10):
if (min1[i] <= 2 or min2[i] <= 2):
if ((abs(min1[i] - min2[i])) < 10):
return i
for i in range(10):
if (min1[i] <= 4 or min2[i] <= 4):
if (abs(min1[i] - min2[i]) <= 2):
return i
for i in range(10):
flag = False
if (min1[i] <= 3 or min2[i] <= 3):
for j in range(10):
if (j != i and (min1[j] < 5 or min2[j] <5)):
flag = True
else:
pass
if (not flag):
return i
for i in range(10):
if (min1[i] <= 5 or min2[i] <= 5):
if (abs(min1[i] - min2[i]) <= 10):
return i
for i in range(10):
if (min1[i] <= 10 or min2[i] <= 10):
if (abs(min1[i] - min2[i]) <= 3):
return i
#end of function Get_Num
def Pic_Reg(image_name=None):
im = Image.open(image_name)
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(5)
im = im.convert('1')
im.show()
#all by pixel
s = 6 #start postion of first number
w = 8 #width of each number
h = 15 #end postion from top
t = 2 #start postion of top
im_new = []
#split four numbers in the picture
for i in range(4):
im1 = im.crop((s+w*i+i*2,t,s+w*(i+1)+i*2,h))
im_new.append(im1.resize((int(10),int(13)), Image.ANTIALIAS))
s = ""
for k in range(4):
l = []
im_new[k].show()
for i in range(13):
for j in range(10):
if (im_new[k].getpixel((j,i)) == 255):
l.append(0)
else:
l.append(1)
s+=str(Get_Num(l))
return s
if __name__ == "__main__":
print(Pic_Reg("3.jpeg"))
的代碼可以讀一些數字,但它並不完美。我需要更好的號碼識別。
到目前爲止你有什麼? – kindall 2011-12-27 16:57:25