我想使用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"))
代码可以读取一些数字,但并不完美。我需要更好的数字识别。
答案 0 :(得分:6)
PIL不是您要使用的工具。你要做的是执行图像识别,这是一个典型的[而且相当复杂的] AI任务。
用于执行此类操作的规范的free-as-free-free库是OpenCV,您可以在其中找到python绑定文档here。
BTW:验证码使用这种图像的原因正是因为教授机器如何识别这些数字非常困难......这是Turing testing的一种形式,专门用于从机器人中识别人类。 ..