图片验证码灰度化、二值化处理

import tesserocr
from PIL import Image

image = Image.open('code.jpg')
image = image.convert('L')
threshola = 127
table = []
for i in range(256):
    if i < threshola:
        table.append(0)
    else:
        table.append(1)

image = image.point(table, '1')
image.show()
result = tesserocr.image_to_text(image)
print(result)

  

from PIL import  Image
import subprocess

image = Image.open('code.jpg')
image = image.point(lambda x: 0 if x<127 else 255)
image.save('code2.jpg')

subprocess.call(["tesseract", 'code2.jpg', "output"])

with open('output.txt', 'r') as f:
    print(f.read())

  

posted @ 2018-08-26 17:14  沙漠bus  阅读(1467)  评论(0)    收藏  举报