gpt4 book ai didi

python - 使用python PIL获取图像中显示的数字

转载 作者:行者123 更新时间:2023-11-28 21:28:18 25 4
gpt4 key购买 nike

我想使用 python 和 PIL 获取此图像中的数字:

enter image description here我的代码:

#!/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"))

代码可以读取一些数字,但并不完美。我需要更好的数字识别。

最佳答案

PIL 不是您要使用的工具。您要做的是执行图像识别,这是一项典型的[并且相当复杂的] AI 任务。

用于执行此类操作的规范的 free-as-in-freedom 库是 OpenCV ,您可以找到 python 绑定(bind)文档 here .

顺便说一句:验证码使用那种图像的原因恰恰是因为教机器如何识别这些数字是极其困难的……它是 Turing testing 的一种形式。专为区分人类和机器人而设计...

关于python - 使用python PIL获取图像中显示的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8647005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com