python 图片验证码识别

采用pytesseract解决,属于 Python 当中比较简单的OCR识别库。

使用pytesseract之前,你需要通过 pip 安装一下对应的模块

pip install pytesseract
pip install pillow

如果你安装了这两个库之后,编写一个识别代码,发现会报错时,你需要

安装一个 Tesseract-OCR 软件。这个软件是由 Google 维护的开源的 OCR 软件。

下载地址 > https://github.com/tesseract-ocr/tesseract/wiki

中文包的下载地址 > https://github.com/tesseract-ocr/tessdata

选择你需要的版本进行下载即可

pillow 库的基本操作
命令解释
open()打开一个图片
from PIL import Image
im = Image.open(“1.png”)
im.show()
save()

保存文件

image.save('b.png',None)

convert()convert() 是图像实例对象的一个方法,接受一个 mode 参数,用以指定一种色彩模式,mode 的取值可以是如下几种:
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· L (8-bit pixels, black and white)
· P (8-bit pixels, mapped to any other mode using a colour palette)
· RGB (3x8-bit pixels, true colour)
· RGBA (4x8-bit pixels, true colour with transparency mask)
· CMYK (4x8-bit pixels, colour separation)
· YCbCr (3x8-bit pixels, colour video format)
· I (32-bit signed integer pixels)
· F (32-bit floating point pixels)

Filter

from PIL import Image, ImageFilter
im = Image.open(‘1.png’)
# 高斯模糊
im.filter(ImageFilter.GaussianBlur)
# 普通模糊
im.filter(ImageFilter.BLUR)
# 边缘增强
im.filter(ImageFilter.EDGE_ENHANCE)
# 找到边缘
im.filter(ImageFilter.FIND_EDGES)
# 浮雕
im.filter(ImageFilter.EMBOSS)
# 轮廓
im.filter(ImageFilter.CONTOUR)
# 锐化
im.filter(ImageFilter.SHARPEN)
# 平滑
im.filter(ImageFilter.SMOOTH)
# 细节
im.filter(ImageFilter.DETAIL)

format 属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为 None;
size 属性是一个 tuple,表示图像的宽和高(单位为像素);
mode 属性为表示图像的模式,常用的模式为:L 为灰度图,RGB 为真彩色,CMYK 为 pre-press 图像。如果文件不能打开,则抛出 IOError 异常。

import pytesseract
from PIL import Image

def main():
    image = Image.open("a.jpg")

    text = pytesseract.image_to_string(image,lang="chi_sim")
    print(text)

if __name__ == '__main__':
    main()

如果你的图片是带有干扰性的验证码时,识别不出来时你需要进行处理,

其基本原理都是完全一样的

  1. 彩色转灰度
  2. 灰度转二值
  3. 二值图像识别
improt tesserocr
from PIL improt Image


image =Image.open(路径)
#将图片转化为灰度图像
image =image.convert('L')
#二值化
threshold=127
table =[]
for i in range(256):
	if i < threshold:
		table.append(0)
	else:
		table.append(1)
image.image.point(table,'1')
result = tesserocr.image_to_text(image)
print(result)

#将部分进行简单封装

import pytesseract
from PIL import Image


def initTable(threshold=127):
    #将图片转化为灰度图像
    #image =image.convert('L')
    #二值化
    #threshold=127
    table =[]
    for i in range(256):
	    if i < threshold:
		    table.append(0)
	    else:
		    table.append(1)
    return table

image =Image.open(图片路径)
#将图片转化为灰度图像
image =image.convert('L')
image=image.point(initTable,'1')
result = tesserocr.image_to_text(image)
print(result)




 普通没有干扰的验证码,我们直接识别即可,但是有的验证码还是有干扰的,在识别之前,需要对它进行基本的处理,我们采用和上面代码类似的办法进行,对它进行灰度处理和二值化操作。部分代码我直接硬编码了,不过最终识别的效果并没有比想象的优化多少。

class Image_opt:
    def init_table(self,threshold=128):
	table = []
	for i in range(256):
		if i < threshold:
			table.append(0)
		else:
			table.append(1)
	return table



    def opt_image(self):
	    im = Image.open("66.png")
	    im = im.convert('L')  #将图片转化为灰度图像
	    im = im.point(self.init_table(), '1')  
	    im.save('66_s.png')   #保存图片
	    return "66_s.png"

调用验证码接口

def get_file_content(self,file_path):
	with open(file_path, 'rb') as fp:
		base64_data = base64.b64encode(fp.read())
		s = base64_data.decode()

		data = {}
		data['image'] = s

		decoded_data = urllib.parse.urlencode(data)
		return decoded_data


def show_code(self):
	image = self.get_file_content(self.opt_image())
	headers = {
		"Content-Type":	"application/x-www-form-urlencoded"
	}
	res = requests.post(self.api.format(self.get_accesstoken()),headers=headers,data=image)
	print(res.text)

通过百度模块调用验证码识别,首先还是要安装模块

pip install baidu-aip

 

from aip import AipOcr


# 定义常量
APP_ID = '15736693'
API_KEY = '你的KEY'
SECRET_KEY = '你的SECRET'

# 初始化文字识别
aipOcr=AipOcr(APP_ID, API_KEY, SECRET_KEY)

# 读取图片
filePath = "1.jpg"

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

# 定义参数变量
options = {
    'detect_direction': 'true',
    'language_type': 'CHN_ENG',
}

# 网络图片文字文字识别接口
result = aipOcr.webImage(get_file_content(filePath),options)


print(result)

借用第三方平台解决验证码 

 

def get_accesstoken(self):
        res = requests.post(self.url.format(self.key,self.secret),headers=self.header)
        content = res.text
        if (content):
            return json.loads(content)["access_token"]


import requests
import json

import base64

import urllib.request, urllib.parse

class GetCode(object):

    def __init__(self):
        self.url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}"
        self.api = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={}"
        self.header = {
            "Content-Type":'application/json; charset=UTF-8'
        }

        self.key = "你的KEY"
        self.secret = "你的SECRET"	

 

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SzetoZeZe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值