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

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

zhangsir3年前 (2023-01-10)python338

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版权t6防采集https://mianka.xyz

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

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

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

标签: python
分享给朋友:

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

python scrapy库安装

(1)安装pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scrapy (2) 报错1: building 'twisted.test.raiser' extension...

如何向python 列表中添加元素

Python添加元素有三种方法:append、extend、insertappend:向列表添加元素,添加到尾部实例:list=[“my”,“name”,“is”,“mark”,“age”,18] print(“添加前:”,list) list.append(“test”) print(“添加...

宝塔面板如何部署Django项目

宝塔面板如何部署Django项目

添加宝塔面板插件登录宝塔面板,进入软件商店,搜索“python项目管理器”然后点击安装进入python项目管理器,点击版本管理,安装版本(注:千万不要添加项目)然后添加网站,php不用所以选静态,添加好了,上传本地的源码。本地源码里没有requirements.txt文件,需要输入命令生成。命令如下...

如何用python获取一个网页的所有连接

如何用python获取一个网页的所有连接很简单直接上代码:# -*- coding: utf-8 -*- ''' 如何用python获取一个网页的所有连接 author:zhangsir ''' imp...

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

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

计算机学习视频教程

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