java验证码去噪_python简单识别验证码去噪

importtesserocrfrom PIL importImage, ImageDrawimporttime#image = Image.open("img/4_1.png")#fh = open("img/1.txt", "w")#w, h = image.size#图片转文本,测试用#for i in range(h):#for j in range(w):#cl = image.getpixel((j, i))#clall = cl[0] + cl[1] + cl[2]## clall == 0即当前像素为黑色#if clall == 0:#fh.write("0")#else:#fh.write("1")#fh.write("\n")#fh.close()

#将图片转为黑白二色

defblack_white(image):

w, h=image.sizefor i inrange(h):for j inrange(w):

cl=image.getpixel((j, i))

35509f5c1dd6cb4407a552fee3d2e022.gif

clall= cl[0] + cl[1] + cl[2]#clall == 0即当前像素为黑色

if clall >= 155*3: # 根据具体的图片修改

image.putpixel((j, i), (255, 255, 255))else:

image.putpixel((j, i), (0, 0, 0))#二值数组

t2val ={}deftwoValue(image,G):for y in range(0,image.size[1]):for x inrange(0,image.size[0]):

g=image.getpixel((x,y))if g >G:

t2val[(x,y)]= 1

else:

t2val[(x,y)]=0#降噪#根据一个点A的RGB值,与周围的8个点的RBG值比较,设定一个值N(0

defclearNoise(image,N,Z):for i inrange(0,Z):

t2val[(0,0)]= 1t2val[(image.size[0]- 1,image.size[1] - 1)] = 1

for x in range(1,image.size[0] - 1):for y in range(1,image.size[1] - 1):

nearDots=0

L=t2val[(x,y)]if L == t2val[(x - 1,y - 1)]:

nearDots+= 1

if L == t2val[(x - 1,y)]:

nearDots+= 1

if L == t2val[(x- 1,y + 1)]:

nearDots+= 1

if L == t2val[(x,y - 1)]:

nearDots+= 1

if L == t2val[(x,y + 1)]:

nearDots+= 1

if L == t2val[(x + 1,y - 1)]:

nearDots+= 1

if L == t2val[(x + 1,y)]:

nearDots+= 1

if L == t2val[(x + 1,y + 1)]:

nearDots+= 1

if nearDots

t2val[(x,y)]= 1

defsaveImage(filename,size):

image= Image.new("1",size)

draw=ImageDraw.Draw(image)for x inrange(0,size[0]):for y in range(0,size[1]):

draw.point((x,y),t2val[(x,y)])

image.save(filename)defstart(img_path,save_img_path):

image=Image.open(img_path)

black_white(image)

image= image.convert("L")

twoValue(image,100)

clearNoise(image,4,1)

saveImage(save_img_path,image.size)print(tesserocr.file_to_text(save_img_path))

img_path= "img/4.png"save_img_path= "img/4_1.png"start(img_path, save_img_path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值