当前位置:首页 > python > 正文内容

python 使用PIL库进行验证码清晰处理

zhangsir3年前 (2023-01-10)python136

python 使用PIL库进行验证码清晰处理

from PIL import Image
import sys
import os
sys.setrecursionlimit(1000000)
pixel_list = []
all_pixel_list = []
#二值化
def Binarization(image):
    threshold = 160
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table, '1')
    return image
def partition(image):
    width = image.size[0]
    height = image.size[1]
    count = 1
    for w in range(width):
        for h in range(height):
            pixel = image.getpixel((w, h))
            if pixel == 0:
                zuobiao = str(w) + ',' + str(h)
                if zuobiao not in all_pixel_list:
                    print('起始像素', zuobiao)
                    if len(pixel_list) > 20:
                        pixel_list.clear()
                    all_pixel_list.append(zuobiao)
                    pixel_list.append(zuobiao)
                    check_around(w, h)
                    print(pixel_list)
                    if len(pixel_list)  > 20:
                        count += 1
                    else:
                        if count != 1:
                            pass
def check_around(width,height):
    shang = ( width , height-1)
    xia = ( width , height+1 )
    zuo = ( width-1, height)
    you = ( width+1 , height)
    if image.getpixel(shang) == 0:
        zuobiao = str(shang[0]) + ',' + str(shang[1])
        if zuobiao not in all_pixel_list:
            all_pixel_list.append(zuobiao)
            pixel_list.append(zuobiao)
            check_around(shang[0], shang[1])
    if image.getpixel(xia) == 0:
        zuobiao = str(xia[0])+','+str(xia[1])
        if zuobiao not in all_pixel_list:
            all_pixel_list.append(zuobiao)
            pixel_list.append(zuobiao)
            check_around(xia[0],xia[1])
    if image.getpixel(zuo) == 0:
        zuobiao = str(zuo[0])+','+str(zuo[1])
        if zuobiao not in all_pixel_list:
            all_pixel_list.append(zuobiao)
            pixel_list.append(zuobiao)
            check_around(zuo[0],zuo[1])
    if image.getpixel(you) == 0:
        zuobiao = str(you[0])+','+str(you[1])
        if zuobiao not in all_pixel_list:
            all_pixel_list.append(zuobiao)
            pixel_list.append(zuobiao)
            check_around(you[0],you[1])
if __name__ == '__main__':
    path = os.path.dirname(__file__) +'jpg/'
    out_path = os.path.dirname(__file__) +'out_jpg/'
    for filename in os.listdir(path):
        image = Image.open(path + filename)
        image = image.convert('L')
        id = filename.split('.')[0]
        image = Binarization(image)
        image.save(out_path + '\{}_二值化.jpg'.format(id))
        partition(image)
        all_pixel_list.clear()


下载验证码程序

# 下载验证码
import requests
import os
path = os.path.dirname(__file__)
for i in range(10):
    url = 'https://my.cnki.net/Register/CheckCode.aspx?id=1605429917005'
    response = requests.get(url)
    file_path = path + 'jpg/{}.jpg'.format(i)
    with open(file_path,'wb') as f:
        f.write(response.content)


zhangsir版权c2防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://www.mianka.xyz/post/95.html

标签: python
分享给朋友:

“python 使用PIL库进行验证码清晰处理” 的相关文章

解决Django的request.POST获取不到请求参数的问题

这个是Django自身的问题:只要在请求头的添加"content-type":'application/x-www-form-urlencoded'就行。...

python selenium find_element_by_xpath 方法已经被弃用的解决办法

背景:在使用最新3.10.4Python版本时候,用selenium进行xpath定位元素,编译器提示:DeprecationWarning:find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value...

python 实现彩色图转素描图

python可以把彩色图片转化为铅笔素描草图,对人像、景色都有很好的效果。而且只需几行代码就可以一键生成,适合批量操作,非常的快捷。需要的第三方库:Opencv - 计算机视觉工具,可以实现多元化的图像视频处理,有Python接口""" Photo ...

python 将json数据转成csv文件

从JSON数据转化CSV文件下面的这个Python脚本能够将JSON数据转化到CSV文件的表格当中去,我们输入的是带有.json后缀的文件,输出的是.csv后缀的表格文件,代码如下import json def converter(input_file, output...

计算机学习视频教程

人工智能机器学习:Python&R实践课程介绍:https://www.aihorizon.cn/1百度网盘地址: https://pan.baidu.com/s/1a743NTKFRjsgexMTagWooA?pwd=e39j动手使用Python进行自然语言处理(NLP)课程介绍:http...