python 图片真伪_【python】图像识别之图片转文字(验证码)

问题:pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

1.安装pip install pytesseract

安装的时候选择把chi_sim(中文简体)和chi_tra(中文繁体)数据库安装上

3.设置环境变量问题:pytesseract.pytesseract.TesseractError: (1, 'Error opening data file C:\\Program Files (x86)\\Tesseract-OCR/eng.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory. Failed loading language \'eng\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')

解决方法:

在.py文件中加入配置:tessdata_dir_config ='--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'

代码:

import time

time1 = time.time()

from PILimport Image

import pytesseract

tessdata_dir_config ='--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'

###########二值化算法

def binarizing(img, threshold):

pixdata = img.load()

w, h = img.size

for yin range(h):

for xin range(w):

if pixdata[x, y] < threshold:

pixdata[x, y] =0

else:

pixdata[x, y] =255

return img

image = Image.open(r'E://pythonprogram//data//image2.png')

###########去除干扰线算法

def depoint(img):# input: gray image

pixdata = img.load()

w, h = img.size

for yin range(1, h -1):

for xin range(1, w -1):

count =0

if pixdata[x, y -1] >245:

count = count +1

if pixdata[x, y +1] >245:

count = count +1

if pixdata[x -1, y] >245:

count = count +1

if pixdata[x +1, y] >245:

count = count +1

if count >2:

pixdata[x, y] =255

return img

# 转化为灰度图

img = image.convert('L')

# 把图片变成二值图像。

img1 = binarizing(img, 190)

# img2=depoint(img1)

img1.show()

code = pytesseract.image_to_string(img1,config=tessdata_dir_config)

print(str(code))

结果:

验证码图片

输出结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值