爬虫验证码灰度化,二值化操作

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/1/18 20:51
# @Author : Lhtester
# @Site :
# @File : 灰度化.py
# @Software: PyCharm

import pytesseract
from PIL import Image

image= Image.open('./data/ChechCode_02.jpg')
text = pytesseract.iamge_to_string(image)

#模式L为灰色图像,它的每个像素用8个BIT表示
#0表示黑,255表示白,其他数字表示不同的灰度
image = image.convert('L')
#黑白化处理,自定义灰度界限,大于这个值为黑色,小于这个值为白色
threshould = 150
table = []
for i in range(256):
if i<threshould:
table.append(0)
else:
table.append(1)
#图片二值化
image = image.point(table,'1')
#保存
image.save('./data/ChechCode_01.jpg')


















posted @ 2022-01-18 21:24  安好_世界  阅读(93)  评论(0编辑  收藏  举报