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

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

1.安装pip install pytesseract

2..安装tesseract-ocr,下载地址:https://github.com/UB-Mannheim/tesseract/wiki

安装的时候选择把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))


结果:

验证码图片
输出结果

你可能感兴趣的:(【python】图像识别之图片转文字(验证码))