Python生成图形验证码

文章目录

安装pillow

pip install pillow

基本用法

特殊字体文字

如下所示,将下载下来的ttf字体文件放到py文件同一文件夹下

分享一个免费下载字体网站:http://www.webpagepublicity.com/free-fonts.html

我选的字体是Bajoran Regular.ttf

python 复制代码
# 第一个参数:表示字体文件路径
# 第二个参数:表示字体大小
font = ImageFont.truetype("Bajoran Regular.ttf", 28)
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
# 第四个参数:表示颜色
draw.text([0, 0], 'python', "red", font=font)

生成图片

python 复制代码
from PIL import Image
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))

# 在图片查看器中查看
img.show()

# 保存到本地
with open('code.png', 'wb') as f:
    img.save(f, format='png')

操作画笔

python 复制代码
#开始画画
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')

# 第一个参数表示坐标,第二个参数表示颜色
draw.point([100, 100], fill="red")
draw.point([300, 300], fill=(255, 255, 255))

# 第一个参数表示起始坐标和结束坐标,第二个参数表示颜色
draw.line((100, 100, 100, 100), fill="red")
draw.line((100, 100, 300, 100), fill=(255, 255, 255))

# 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
# 第二个参数:表示开始角度
# 第三个参数:表示结束角度
# 第四个参数:表示颜色
draw.arc((100,100,300,300),0,90,fill="red")

生成代码

python 复制代码
import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter


# 生成默认含4个字符验证码的图片
def check_code(width=120, height=30, char_length=4, font_file='Bajoran Regular.ttf', font_size=28):
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')

    def rndChar():
        """
        生成随机大小写字母
        :return:
        """
        up_or_low = random.randint(0, 1)
        if up_or_low == 0:
            return chr(random.randint(97, 122))
        return chr(random.randint(65, 90))

    def rndColor():
        """
        生成随机颜色
        :return:
        """
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))

    # 写文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        char = rndChar()
        code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

    # 写干扰点
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

    # 写干扰圆圈
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

    # 画干扰线
    for i in range(5):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)

        draw.line((x1, y1, x2, y2), fill=rndColor())

    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    return img, ''.join(code)


if __name__ == '__main__':
    img, code_str = check_code()
    print(code_str)

    with open('code.png', 'wb') as f:
        img.save(f, format='png')

运行结果

相关推荐
认真的酒窝4 小时前
自己动手开发编译器(十一)语义分析
java·开发语言
Lyn_Li4 小时前
扫描 PDF 歪了怎么办?用 6 种检测方法做本地批量扶正(附开源工具)
python·pdf·ocr·tesseract·开源工具·文档处理·本地处理·扫描件纠偏
金銀銅鐵4 小时前
费马小定理
python·数学·算法
wbs_scy5 小时前
Linux C++ 高并发编程:线程池全链路深度解析,从原理到手撕实现
java·开发语言
JAVA面经实录9175 小时前
Linux 常用命令完整知识体系
java·linux·开发语言·汇编
贪玩的蛋挞6 小时前
C#与闭包
开发语言·c#
端庄的战斗机6 小时前
javascript 设计模式(文章很长,请自备瓜子,水果和眼药水)
开发语言·javascript·设计模式
Full Stack Developme7 小时前
Java LRU 与 LFU 算法及应用
java·开发语言·算法
疋瓞8 小时前
python和C++对比(1)_数据类型和数据结构
数据结构·c++·python
C语言小火车8 小时前
C++ 堆排序深度精讲:基于完全二叉树的选择排序进化,最坏情况 O(n log n) 的稳定王者
开发语言·c++·算法·排序算法·堆排序