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

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

zhangsir3年前 (2023-01-10)python331

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

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

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

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

标签: python
分享给朋友:

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

权大师商标查询api

''' data:2022-10-15 autor:zhangsir 权大师商标查询api ''' import requests import json import time import h...

python 实现彩色图转素描图

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

计算机学习视频教程

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

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

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

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

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

Linux之python版本升级

四、python3升级步骤1、下载安装包wget https://www.python.org/ftp/python/3.8.8/Python-3.8.8.tgz2、解压软件包tar -zxvf Python-3.8.8.tgz3、预编译注意在编译结束后会有提示&quo...