Python黑科技03-练手小项目-随机生成卡片式验证码

简介: 每个人根据自己生长环境的不同;接触事物的不同;对一些事情认知的眼界也不同。而在成长的路途上每个人都会遇到很多的磨难及坎坷,有让自己开心的,也有让自己糟糕的。仔细想想其实大家的竞争对手从来就不是其他任何人。 你的竞争对手是你的拖延, 你的自负 , 你正在消费的不健康饮食, 你的懒惰 以及你正在养成的环习惯和思维上的墨守成规。 如果能克服它们的话, 你还怕谁 ? !

效果演示

image.png

图像的背景颜色是可以自行调节成随机颜色的,这里只演示一下。大家可在后面进行学习。
image.png

安装模块:

  • pip install pillow
  • PIL是python2中提供的一个图像数据处理的模块,但在python3中更名为了pillow模块,名字虽然发生了改变,但是提供的方法和功能都是一样的

-实战

思路 :

一张验证码所需要具备的因素:

  • 1、规范验证码背景图片的尺寸大小
  • 2、规范字体大小及要显示字体的个数
  • 3、设置背景及线条个数、增加识别难度

第一步:设置一张自定义大小的背景图

from PIL import Image

 # 生成一张图片
 captcha = Image.new('RGB', size=(150,100), color=(255, 180, 120))
 # 保存图片
 captcha.save('captcha.png')

image.png

第二步:填充背景图的颜色

from PIL import Image, ImageDraw
import random

# 颜色随机 
def get_line_color():
    line_color = (random.randint(0, 250), random.randint(0, 255), random.randint(0, 250))
    return line_color
    
# 绘制小圆点
def draw_point(draw, point_chance, width, height):
    '''
    :param draw: 画笔对象
    :param point_chance: 绘制小圆点分布的几率 概率为 point_chance/100
    :param width: 验证码宽度
    :param height: 验证码高度
    :return:
    '''
    # 按照概率随机绘制小圆点
    for w in range(width):
        for h in range(height):
            tmp = random.randint(0, 100)
            if tmp < point_chance:
                draw.point((w, h), fill=get_line_color())
if __name__ == '__main__':
    captcha_size = (150, 100)
    rgb_color=(255, 180, 120)
    # 生成一张图片
    captcha = Image.new('RGB', size=captcha_size, color=rgb_color)
    width, height = captcha_size
    # 获取画笔对象
    draw = ImageDraw.Draw(captcha)
    draw_point(draw, 10, width, height)
    # 保存图片
    captcha.save('captcha.png')

image.png

OK 目前为止代码的逻辑还并不是很难;接下来就是设置字体;添加字体的同时然后在顺路添加一些纵横线什么的;加大识别难度。

接下来在写代码的同时我会把代码封装成函数或者类进行书写。也是为了更加方便大家的阅读性,所以下面代码和上面会有一些重复。

第三步:添加文字及线条,增加识别难度

from PIL import Image, ImageDraw, ImageFont

# 绘制线条
def draw_line(draw, captcha_width, captcha_height):
    '''
    :param draw: 画笔对象
    :param captcha_width: 验证码的宽度
    :param captcha_height: 验证码的高度
    :return:
    '''
    # 随机获取开始位置的坐标
    begin = (random.randint(0,captcha_width/2), random.randint(0, captcha_height))
    # 随机获取结束位置的坐标
    end = (random.randint(captcha_width/2,captcha_width), random.randint(0, captcha_height))
    draw.line([begin, end], fill=get_line_color())
    
# 按照RGB风格设置字体颜色
def get_font_color():
    font_color = (random.randint(0, 150), random.randint(0, 150), random.randint(0, 150))
    return font_color
    
# 得到绘制的字符
def get_text(sources=None):
    if sources:
        sources = sources
    else:
        sources = string.ascii_letters + string.digits
    text = random.sample(sources,k=text_number)
    return ''.join(text)
  # 展示绘制的字体
 def draw_text(draw, text, font, captcha_width, captcha_height, spacing=20):
    '''
    :param draw: 画笔对象
    :param text: 绘制的所有字符
    :param font: 字体对象
    :param captcha_width: 验证码的宽度
    :param captcha_height: 验证码的高度
    :param spacing: 每个字符的间隙
    :return:
    '''
    # 得到这一窜字符的高度和宽度
    text_width, text_height = font.getsize(text)
    # 得到每个字体的大概宽度
    every_value_width = int(text_width / 4)

    # 这一窜字符的总长度
    text_length = len(text)
    # 每两个字符之间拥有间隙,获取总的间隙
    total_spacing = (text_length-1) * spacing

    if total_spacing + text_width >= captcha_width:
        raise ValueError("字体+中间的空隙超过了图片宽度!")

    # 获取第一个字符绘制位置
    start_width = int( (captcha_width - text_width - total_spacing) / 2 )
    start_height = int( (captcha_height - text_height) / 2 )

    # 依次绘制每个字符
    for value in text:
        position = start_width, start_height
        print(position)
        # 绘制text
        draw.text(position, value, font=font, fill=get_font_color())
        # 改变下一个字符的开始绘制位置
        start_width = start_width + every_value_width + spacing
        
 if __name__ == '__main__':
    # 背景图大小尺寸
    captcha_size = (150, 100)
    # 背景颜色
    rgb_color=(255, 180, 120)
    # 字体大小
    font_size = 30
    # 展示图片最大数量
    text_number = 4

    # 生成一张图片
    captcha = Image.new('RGB', size=captcha_size, color=rgb_color)
    width, height = captcha_size
    # 获取画笔对象
    draw = ImageDraw.Draw(captcha)
    draw_point(draw, 10, width, height)
    # 得到绘制字体
    text=get_text()
    # 获取字体对象
    font = ImageFont.truetype('simkai.ttf', font_size)
    draw_text(draw, text, font, width, height)

    # 保存图片
    captcha.save('captcha.png')

image.png

至此,大体上的框架逻辑实现的差不多了,接下来在添加上一些纵横交错的线条;看一下

# 绘制线条
def draw_line(draw, captcha_width, captcha_height):
    '''
    :param draw: 画笔对象
    :param captcha_width: 验证码的宽度
    :param captcha_height: 验证码的高度
    :return:
    '''
    # 随机获取开始位置的坐标
    begin = (random.randint(0,captcha_width/2), random.randint(0, captcha_height))
    # 随机获取结束位置的坐标
    end = (random.randint(captcha_width/2,captcha_width), random.randint(0, captcha_height))
    draw.line([begin, end], fill=get_line_color())
    
 if __name__ == '__main__':
 
    # 线条数量
    line_number=4

    # 绘制线条
    for i in range(line_number):
        draw_line(draw, width, height)
    # 保存图片
    captcha.save('captcha.png')

image.png

哈哈 效果还可以,很赞! 背景颜色也是可以设置随机的 ,我这里设置成了固定的颜色,大家在学习的过程中可以自行修改。

在这个浮躁的时代;竟然还有人能坚持篇篇原创;

如果本文对你学习有所帮助-可以点赞👍+ 关注!将持续更新更多新的文章。

支持原创。感谢!

相关文章
|
1月前
|
机器学习/深度学习 人工智能 开发工具
人工智能项目的python版本管理工具DVC
dvc即data version control, 是一种针对人工智能项目(机器学习或者深度学习)的数据版本管理工具。DVC的操作和GIT类似,可以认为为GIT的二次开发封装。结合GIT,DVC可以有效的管理人工智能项目的整个流程,包括代码,数据,训练配置,模型【2月更文挑战第12天】
60 1
|
1月前
|
数据采集 机器学习/深度学习 安全
Python爬虫之极验滑动验证码的识别
了解极验滑动验证码、特点、识别思路、初始化、模拟点击、识别缺口、模拟拖动。
60 0
|
25天前
|
前端开发 JavaScript 数据管理
描述一个使用Python开发Web应用程序的实际项目经验,包括所使用的框架和技术栈。
使用Flask开发Web应用,结合SQLite、Flask-SQLAlchemy进行数据管理,HTML/CSS/JS(Bootstrap和jQuery)构建前端。通过Flask路由处理用户请求,模块化代码提高可维护性。unittest进行测试,开发阶段用内置服务器,生产环境可选WSGI服务器或容器化部署。实现了用户注册登录和数据管理功能,展示Python Web开发的灵活性和效率。
14 4
|
1月前
|
数据采集 Web App开发 文字识别
Python爬虫之点触验证码的识别
点触验证码识别思路,初始化,获取,识别。
56 0
Python爬虫之点触验证码的识别
|
1月前
|
数据采集 文字识别 开发者
Python爬虫之图形验证码的识别
python爬虫逆向图形验证码分析,处理和测试实战。
46 0
|
1月前
|
机器学习/深度学习 人工智能 文字识别
Python常用验证码标注和识别(需求分析和实现思路)
Python常用验证码标注和识别(需求分析和实现思路)
46 0
|
1月前
|
Linux 数据库连接 数据库
Python如何将项目直接打包为一键整合包
Python如何将项目直接打包为一键整合包
41 0
|
1月前
|
存储 安全 JavaScript
使用Python的Flask框架开发验证码登录功能
使用Python的Flask框架开发验证码登录功能
28 0
|
1月前
|
数据采集 存储 数据处理
Python爬虫在Django项目中的数据处理与展示实例
Python爬虫在Django项目中的数据处理与展示实例
|
1月前
|
人工智能 开发工具 git
第一次运行 Python 项目,使用 python-pptx 提取 ppt 中的文字和图片
人工智能时代,最需要学习的编程语言是:python 。笔者是个 python 小白,昨天花了两个小时,第一次成功运行起来 python 项目 。 项目是 powerpoint-extractor ,可以将 ppt 文件中的图片提取出来,并输出到固定的目录。
第一次运行 Python 项目,使用 python-pptx 提取 ppt 中的文字和图片