Reputation: 143
I want get the number in this image, using python and PIL:
my code:
#!/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"))
The code can read some numbers, but it isn't perfect. I need better number recognition.
Upvotes: 2
Views: 5030
Reputation: 43031
PIL is not the tool you want to use. What you are trying to do is perform image recognition, which is a typical [and quite complex] AI task.
The canonical free-as-in-freedom library for performing this kind of operations is OpenCV, for which you can find the python bindings documentation here.
BTW: the reason for which captcha use that kind of images is precisely because is extremely difficult to teach a machine how to recognise those numbers... It's a form of Turing testing specifically designed to discern humans from bots...
Upvotes: 6