zl程序教程

您现在的位置是:首页 >  后端

当前栏目

selenium python pytesseract 二值化图像

Python 图像 selenium 二值化
2023-09-14 09:09:51 时间

1. 报错AttributeError: ‘str’ object has no attribute ‘save’

# coding:utf-8
# coding:cp936
import pytesseract
from PIL import Image
import os

img = Image.open(r'C:\picture2.png')
img = img.convert('RGBA')
pix = img.load()
for x in range(img.size[0]):
    pix[x, 0] = pix[x, img.size[1] - 1] = (255, 255, 255, 255)
for y in range(img.size[1]):
    pix[0, y] = pix[img.size[0] -1, y] = (255, 255, 255,255)
# img.save(r'C:\picture2_1.png')

for y in range(img.size[1]):
    for x in range(img.size[0]):
        if pix[x, y][0] < 150 or pix [x, y][1] < 150 or pix[x, y][2] < 150:
            # pix[x, y] = (0, 0, 0, 255)
            pix[x, y] = (255, 255, 255, 255)
        else:
            pix[x, y] = (0, 0, 0, 255)
img.save(r'C:\picture2_4.png')
test = pytesseract.image_to_string(r'C:\picture2_4.png')
# os.remove(r'C:\picture2_4.png')
print test

2. 报错解决

from pytesser import * 

im = Image.open(r'C:\picture2_4.png')
text = image_to_string(im) 
print text

参考:
1.Python:利用tesseract自动识别网站验证码
2.wxPython利用pytesser模块实现图片文字识别