首页 > 编程语言 >【Python小随笔】Pillow简单示例(图片添字,图片覆盖图片,复杂验证码...)

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

时间:2023-08-01 17:01:09浏览次数:29  
标签:... 示例 image random captcha width 128 图片 255

 简单创建添加文字到图片

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

  

 

标签:...,示例,image,random,captcha,width,128,图片,255
From: https://www.cnblogs.com/wanghong1994/p/17596146.html

相关文章

  • 01-[Linux][Regulator]使用LDO编程示例
    1、在驱动代码中使用LDO供电操作的步骤如下:找到需要操作的LDO名字,如MTK平台:vio28在dts中找到相应的节点,如下所示:mt_pmic_vio28_ldo_reg:ldo_vio28{ regulator-name="vio28"; regulator-min-microvolt=<2800000>; regulator-max-microvolt=<2800000>; regulator-e......
  • Unexpected exception 'Cannot run program ... error=2, No such file or directory'
    EclipseADTUnexpectedexception'Cannotrunprogram'......
  • UEditor上传图片word
    ​图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,找到UM.plugins['autoupload'],然后找到autoUploadHandler方法,注释掉其中的代码。加入下面的代码://判断剪贴......
  • UMEditor上传图片word
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • go操作etcd watch示例
    packagemainimport("fmt""go.etcd.io/etcd/Godeps/_workspace/src/golang.org/x/net/context""go.etcd.io/etcd/clientv3""time")funcmain(){cli,err:=clientv3.New(clientv3.Config{Endpoints:[]str......
  • 百度编辑器上传图片word
    ​ 由于工作需要必须将word文档内容粘贴到编辑器中使用但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直接解决这个问题考虑到自己除了工作其他时间基本上不使用windows,因此打算使用nodejs来解......
  • 如果让我手写......
    字符串#include<iostream>#include<cstring>classMyString{private:char*m_data;size_tm_length;public:MyString():m_data(nullptr),m_length(0){}MyString(constchar*str){m_length=strlen(str);m_data=......
  • CSS 文字-背景图片纹理、背景视频效果
    1、背景图片纹理效果实现方案:搬运CSS代码:.pattern-overlay{font-size:60px;font-family:'microsoftyahei';background-image:url(./pattern01.jpg);-webkit-text-fill-color:transparent;-webkit-background-clip:text;}.pattern-overlay......
  • Web编辑器上传图片word
    ​ 在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper。通过知乎提供的思路找到粘贴的原理,通过TheViper找到粘贴图片的方法。其原理为一下步骤:监听粘贴事件;【用于插入图片】获取光标位置;【......
  • ...扩展运算符
     目录前言导语 代码部分 运行结果前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷导语歌谣歌谣......