栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python验证码的识别

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python验证码的识别

 

 

 

 

 可以看到基本上是三种颜色,背景占比最多,其次就是验证码

我们可以把图片灰度化 去除最多的背景颜色 剩下最多的就是想得到的验证码了


灰度化

file = r"D:/photo/8.png"
image = Image.open(file)
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)#加入数组

a=max(im2, key=im2.count)


完整代码

import pytesseract

from PIL import Image

import numpy as np

import cv2 as cv

import cv2

import pytesseract

file = r"D:/photo/8.png"

image = Image.open(file)

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)

print(im2)

a=max(im2, key=im2.count)

print(a)

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'

content = pytesseract.image_to_string(image,config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789')

print(content)

 

小白第一次写 有更好的方法请大佬指教

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/828854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号