python 对图片增加边框,logo贴图,获取图片exif参数,填写图片文本内容

完整代码

python 复制代码
# 找到个可以下载免费字体的网站https://font.chi删除我naz.com/mi删除我anfei.html
from PIL import Image, ImageDraw, ImageFont
import exifread

def photo_exif(image_path):

    f = open(image_path, 'rb')

    tags = exifread.process_file(f)

    # 打印所有照片信息,会以键值对的方法保存
    # for tag in tags.keys():
    #     print("Key: {0}, value {1}".format(tag, tags[tag]))
    # print(str(tags['EXIF FocalLength']) + 'mm', tags['EXIF ExposureTime'], 'ISO' + str(tags['EXIF ISOSpeedRatings']))
    return tags



def add_logo_with_text(image_path, logo_path, logo_size, text1, text2, text3, font_path, font_size, font_color, border_size,
                       border_color, output_path):
    # 打开原始图片
    image = Image.open(image_path).convert("RGB")
    width, height = image.size

    # 计算边框区域大小和位置
    font = ImageFont.truetype(font_path, font_size)
    text1_width, text1_height = font.getsize(text1)
    text2_width, text2_height = font.getsize(text2)
    text3_width, text3_height = font.getsize(text3)
    text_width = max(text1_width, text2_width, text3_width)
    text_height = text1_height + text2_height + text3_height
    border_width = logo_size[0] + text_width + border_size * 3
    border_height = max(logo_size[1], text_height) + border_size * 2
    border_position = ((width - border_width) // 2, height)

    # 打开logo图片并调整大小
    logo = Image.open(logo_path).resize(logo_size, Image.ANTIALIAS)

    # 创建新的图片
    new_width = width
    new_height = height + border_height
    new_image = Image.new("RGB", (new_width, new_height), "white")

    # 将原始图片复制到新图片的顶部
    new_image.paste(image, (0, 0, width, height))

    # 在新图片上绘制边框
    draw = ImageDraw.Draw(new_image)
    border_rect = (border_position[0], height, border_position[0] + border_width, height + border_height)
    draw.rectangle(border_rect, fill=None, outline=border_color, width=border_size)

    # 在边框区域内绘制logo图片
    logo_position = (border_position[0] + border_size, height + (border_height - logo_size[1]) // 2)
    new_image.paste(logo, logo_position)

    # 在边框区域内绘制文本
    text1_position = (border_position[0] + border_size * 2 + logo_size[0], height + (border_height - text_height) // 2)
    text2_position = (border_position[0] + border_size * 2 + logo_size[0], text1_position[1] + text1_height)
    text3_position = (border_position[0] + border_size * 2 + logo_size[0], text2_position[1] + text2_height)
    draw.text(text1_position, text1, font=font, fill=font_color)
    draw.text(text2_position, text2, font=font, fill=font_color)
    draw.text(text3_position, text3, font=font, fill=font_color)

    # 保存合成后的图片
    new_image.save(output_path)


# 示例用法
# 照片路径
image_path = "DSC_1966.jpg"
# logo图片路径
logo_path = "2.png"
# logo图片大小
logo_size = (255, 255)
# 图片信息
tags = photo_exif(image_path)
text1 = "Power For."+str(tags['Image Model'])+"     "+"FL."+str(tags['EXIF FocalLength'])+"mm"+"     "+"EB."+str(tags['EXIF ExposureTime'])+"     "+"ISO."+str(tags['EXIF ISOSpeedRatings'])+"     "+"WL."+str(tags['EXIF ExifImageWidth'])+" x "+str(tags['EXIF ExifImageLength'])
text2 = "DtO." + str(tags['EXIF DateTimeOriginal']) + "     " + "By.林俊杰裤子掉了"
text3 = "尼康,  感动常在 ╰( ̄▽ ̄)╭"
# 字体路径
font_path = "siyuanyuanti.ttf"
font_size = 55
font_color = (0, 0, 0)  # 黑色
border_size = 55
border_color = (255, 255, 255)  # 白色
# 输出照片 .后缀为png为无损图片 ,jpg为压缩后的图片
output_path = "output_image.png"

add_logo_with_text(image_path, logo_path, logo_size, text1, text2, text3, font_path, font_size, font_color, border_size,
                   border_color, output_path)
print("图片已保存至:", output_path)

输出结果

效果图

可自行写成tk界面化选择图片处理,及处理多张图片的功能

相关推荐
hhzz8 小时前
基于监控视频的水位尺自动识别技术方案与实现
python·opencv·yolo·图像识别·cv
yongche_shi8 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
weixin_408099679 小时前
OCR批量识别图片方案:从手动处理到自动化API系统(Python/Java/PHP实战)
图像处理·python·ocr·文字识别·api调用·批量识别·石榴智能
AI行业学习9 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
大圣编程10 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
云烟成雨TD10 小时前
LangFlow 1.x 系列【5】可视化编辑页面功能说明
人工智能·python·agent
geovindu11 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
tryCbest12 小时前
Python 文件操作
服务器·python
涛声依旧-底层原理研究所12 小时前
Agent 长任务可靠性设计:实现暂停、恢复、续跑与崩溃重启的完整方案
人工智能·python·系统架构
AC赳赳老秦12 小时前
防火墙规则批量配置实战:OpenClaw 自动生成模板、批量下发与合规性校验全解析
java·开发语言·人工智能·python·github·php·openclaw