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

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

zhangsir3年前 (2023-01-10)python187

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

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

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

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

标签: python
分享给朋友:

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

python 写入文件

一、读写txt文件1、打开txt文件Note=open('x.txt',mode='w',encoding='utf-8')函数=open(x.扩展名,mode=模式)模式种类:w      ...

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

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

python 给电脑设置闹钟

python会自动触发windows桌面通知,提示重要事项,比如说:您已工作两小时,该休息了我们可以设定固定时间提示,比如隔10分钟、1小时等用到的第三方库:win10toast - 用于发送桌面通知的工具from win10toast import ToastNoti...

python 将json数据转成csv文件

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

python 多线程与多进程的代码实例

一.两者区别多进程和多线程的主要区别是:线程是进程的子集(部分),一个进程可能由多个线程组成。多进程的数据是分开的、共享复杂,需要用IPC;但同步简单。多线程共享进程数据,共享简单;但同步复杂。(1)多进程进程是程序在计算机上的一次执行活动,即正在运行中的应用程序,通常称为进程。当你运行一个程序,你...

python—pymysql的增删改查操作实例展示

Python使用pymysql连接数据库1.导包import pymysql2.连接数据库connection = pymysql.connect(     host='',  # ...