# 图片

图片自动化办公的功能,目前有:

安装:pip install python-office

# 1、给图片添加水印

给图片批量添加文字水印,保护图片版权。

import office

office.image.add_watermark(
    file=r'D:\images\photo.jpg',  # 图片路径或图片所在文件夹
    mark='python-office.com'       # 水印文字内容
)

参数说明:

参数 类型 必填 说明
file str 图片文件路径,或存放图片的文件夹路径
mark str 水印文字内容

# 2、制作动漫头像

文字说明:传送门

用 AI 将真人照片转换为动漫风格头像。

import office

# 默认方式(使用内置 API,有试用次数限制)
office.image.img2Cartoon(path=r'D:\image\my_photo.jpg')

效果展示:

cartoon

提示: 如果报错提示 image 相关错误,说明免费试用次数已用完。需要自行在 百度AI平台 开通「人像动漫化」应用,获取 API Key 后按以下方式使用:

import office

office.image.img2Cartoon(
    path=r'D:\image\my_photo.jpg',
    client_api='your-api-key',       # 百度 API Key
    client_secret='your-secret-key'  # 百度 Secret Key
)

参数说明:

参数 类型 必填 说明
path str 照片文件路径,如 .jpg
client_api str 百度 AI 平台 API Key
client_secret str 百度 AI 平台 Secret Key

# 3、国旗头像生成

为头像添加国旗边框,适用于节假日等特殊场景。

视频说明:传送门

import office

office.image.flag2profile(
    img_path=r'D:\photo.jpg',   # 原始头像路径
    output_path=r'D:\output'    # 输出图片保存目录
)

参数说明:

参数 类型 必填 说明
img_path str 原始头像文件路径
output_path str 输出图片保存路径,默认当前目录

# 相关课程

Last Updated: 4/6/2026, 10:18:15 AM