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

1:简单创建添加文字到图片

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

# 创建一个图片对象    (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")  
 


 

2:生成复杂验证码

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")  

 

3:图片覆盖图片

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")

posted @   PythonNew_Mr.Wang  Views(241)  Comments(0Edit  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示