python验证码识别

 大一时候写的 重新整理一下https://blog.csdn.net/m0_68198946/article/details/124366487?spm=1001.2014.3001.5501

目录

1.首先了解一下python怎样识别图片文本内容:

这里用到python的pytessract库

2.思路:

 识别部分

 设计个UI

完整代码


1.首先了解一下python怎样识别图片文本内容:

这里用到python的pytessract

          图1.png

from PIL import Image
import pytesseract
image = Image.open('图1.png')
# 解析图片,lang='chi_sim'表示识别简体中文,默认为English
# 如果是只识别数字,可再加上参数config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789'
content = pytesseract.image_to_string(image,config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789')
print(content)

输出结果:

1314521

显然验证码图片不能直接这样识别,要去除噪点

2.思路:

显然图片中最多的颜色就是背景色 其次就是验证码的颜色(第二多)

把图片二值化 把验证码的颜色变成白色剩余全是黑色

 ➩ ➩

                   原图                                           灰度化                                      要识别的图片

 识别部分

import pytesseract
from PIL import Image
import numpy as np
import cv2 as cv
import cv2
import pytesseract

a='xxx.png'
image = Image.open(a)
img = image.convert('L')   # 灰度化
im2=[]
cols,rows = img.size
for x in range(0,rows):
    for y in range(0,cols):
        img_array = np.array(img)
        v = img_array[x,y] # 获取该点像素值
        im2.append(v)#加入数组
while 0 in im2:
    im2.remove(0)#删除灰度0
a=max(im2, key=im2.count)#出现最多的数字   背景颜色
while a in im2:
    im2.remove(a)
a=max(im2, key=im2.count)
table = []
for i in range(256):
    if i ==a:
        table.append(1)
    else:
        table.append(0)
photo = img.point(table, '1')  #图片二值化
photo.save('02.jpg')
image = Image.open('02.jpg')
tessedit_char_whitelist=0123456789'
content = pytesseract.image_to_string(image,config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789')
print(content)

 设计个UI

import tkinter as tk
import tkinter.filedialog
gu=tk.Tk()
gu.geometry("200x100")
gu.title("             验证码识别")
def tu():
    kuang.delete(0,"end")  #删除上次识别的数字
    ts=tkinter.filedialog.askopenfilename()  #图片目录
    v.set(识别结果)
an=tk.Button(text="选择",command=tu,bg='skyblue').place(x=130,y=35)
test=tk.Label(text="识别结果",font=("宋体")).place(x=8,y=10)
v = tk.StringVar()
kuang=tk.Entry(width=15,textvariable=v)
kuang.place(x=15,y=40)
gu.mainloop()

效果图

完整代码

import tkinter as tk
import tkinter.filedialog
import pytesseract
from PIL import Image
import numpy as np
import cv2 as cv
import cv2
import pytesseract
def shi(a):
    global jieguo
    image = Image.open(a)
    img = image.convert('L')  # 灰度化
    im2 = []
    cols, rows = img.size
    for x in range(0, rows):
        for y in range(0, cols):
            img_array = np.array(img)
            v = img_array[x, y]  # 获取该点像素值
            im2.append(v)  # 加入数组
    while 0 in im2:
        im2.remove(0)  # 删除灰度0
    a = max(im2, key=im2.count)  # 出现最多的数字   背景颜色
    while a in im2:
        im2.remove(a)
    a = max(im2, key=im2.count)
    table = []
    for i in range(256):
        if i == a:
            table.append(1)
        else:
            table.append(0)
    photo = img.point(table, '1')  # 图片二值化
    photo.save('02.jpg')
    image = Image.open('02.jpg')
    # # 解析图片,lang='chi_sim'表示识别简体中文,默认为English
    # # 如果是只识别数字,可再加上参数config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789'
    jieguo = pytesseract.image_to_string(image, config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789')

gu=tk.Tk()
gu.geometry("200x100")
gu.title("             验证码识别")
def tu():
    kuang.delete(0,"end")  #删除上次识别的数字
    ts=tkinter.filedialog.askopenfilename()  #图片目录
    shi(ts)
    v.set(jieguo)
an=tk.Button(text="选择",command=tu,bg='skyblue').place(x=130,y=35)
test=tk.Label(text="识别结果",font=("宋体")).place(x=8,y=10)
v = tk.StringVar()
kuang=tk.Entry(width=15,textvariable=v)
kuang.place(x=15,y=40)
gu.mainloop()

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值