【Python小随笔】Pillow简单示例(图片添字,图片覆盖图片,复杂验证码...)

发布时间 2023-08-01 16:57:57作者: PythonNew_Mr.Wang

 简单创建添加文字到图片

from PIL import Image, ImageDraw, ImageFont, ImageFilter  # 导入PIL库中的相关模块
import random  # 导入random库

# 简单的添加文字
"""
mode:图片模式,指定了每个像素点的颜色组织方式。常用的模式有:
    '1':二值图像,每个像素点只有黑和白两种颜色。
    'L':灰度图像,每个像素点有256种不同的灰度级别。
    'RGB':真彩色图像,每个像素点由红、绿、蓝三个分量组成。
    'RGBA':带有透明度通道的真彩色图像,每个像素点由红、绿、蓝、透明度四个分量组成。
    size:图片尺寸,以像素为单位的元组,例如(width, height)。
    color:填充颜色,可以是颜色名称、RGB元组或RGBA元组。默认为黑色。
"""
# 创建一个图片对象     (200, 100) 为 图片尺寸
image = Image.new('RGB', (200, 100), 'white')

# 创建一个可以在图片上绘制的对象
new_picture_draw = ImageDraw.Draw(image)

# 加载字体
font = ImageFont.truetype(font="正楷字体.ttf", size=50)  # 加载指定字体文件和字体大小

# 绘制对象 在指定位置绘制验证码文本,颜色为黑色
new_picture_draw.text((89, 44), "Ae86", font=font, fill='black')

# 绘制对象 在指定位置绘制横线 横线为红色
# [(左X坐标, 左Y坐标), (右X坐标, 右Y坐标)]
new_picture_draw.line([(0, 60), (200, 60)], fill="red", width=2)

# 加圆圈
new_picture_draw.ellipse([(30, 30), (60, 60)], outline='blue', width=4)

# 绘制粗点   (x-1, y-1), (x+1, y+1) 是为了点粗
points = [(80, 50), (100, 60), (120, 50)]
for point in points:
    x, y = point
    new_picture_draw.rectangle([(x - 2, y - 2), (x + 2, y + 2)], fill='green')

# 模糊效果   radius设置模糊程度
image = image.filter(ImageFilter.GaussianBlur(radius=2))

# 保存位置
image.save("test.jpg")  

  

生成复杂验证码

import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter

rgb_list = [(255, 0, 0), (0, 255, 0), (0, 0, 255),  (255, 0, 255),
            (0, 255, 255), (128, 128, 128), (255, 128, 0), (128, 0, 255),
            (0, 128, 255), (128, 255, 0), (255, 0, 128), (0, 255, 128), ]

def generate_captcha(width, height, length):
    # 创建画布
    image = Image.new('RGB', (width, height), 'white')
    draw = ImageDraw.Draw(image)

    # 设置字体
    font = ImageFont.truetype('正楷字体.ttf', random.randint(40, 50))

    # 随机生成验证码
    captcha = ''
    for i in range(length):
        char = random.choice('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
        captcha += char
        x = int(width / length * i + random.uniform(0, 10))
        y = random.uniform(0, height - 50)
        draw.text((x, y), char, font=font, fill=random.choice(rgb_list))

    # 添加随机粗点
    for i in range(50):
        x = random.randint(0, width - 1)
        y = random.randint(0, height - 1)
        draw.rectangle([(x, y), (x + 1, y + 1)], fill=random.choice(rgb_list))

    # 添加随机线条
    for i in range(5):
        x1 = random.randint(0, width-1)
        y1 = random.randint(0, height-1)
        x2 = random.randint(0, width-1)
        y2 = random.randint(0, height-1)
        draw.line([(x1, y1), (x2, y2)], fill=random.choice(rgb_list), width=2)

    # 模糊
    image = image.filter(ImageFilter.GaussianBlur(radius=0.6))
    return image, captcha

# 示例用法
captcha_image, captcha_text = generate_captcha(200, 80, 6)
print(captcha_text)
captcha_image.save("pp.jpg")  

  

图片覆盖图片

from PIL import Image

# 打开大图  
big_img = Image.open("原图2.jpg")

# 打开小图
small_img = Image.open("pp.jpg")

# 将小图覆盖在大图上 左上角的位置 => (30,30)
big_img.paste(small_img, (50, 50))

# 保存合成后的图像
big_img.save("output.jpg")