You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Imagemagick使用求助:如何编写Python脚本处理目录内图片与logo生成PPT

解决方案

ImageMagick 常用操作参考

以下是和图片+logo处理相关的高频命令,可直接使用:

  • 给单张图片添加右下角logo水印(边距15px):
    convert 原图路径 logo路径 -gravity southeast -geometry +15+15 -composite 输出图路径
  • 批量调整目录下所有图片宽度到1920px(高度等比缩放,不放大小于1920px的图):
    mogrify -resize 1920x\> *.jpg *.png
  • 批量清除图片EXIF信息:
    mogrify -strip *.jpg *.png
    如果有具体的ImageMagick使用问题(比如特定处理需求、执行报错),可补充对应场景和报错信息。

图片批量生成PPT Python 脚本

依赖安装

执行以下命令安装所需依赖:
pip install python-pptx pillow

脚本代码

import os
import sys
from pptx import Presentation
from pptx.util import Inches, Pt
from PIL import Image

# 可修改配置项
SUPPORTED_IMG_EXTS = ('.jpg', '.jpeg', '.png', '.bmp')
LOGO_KEYWORD = 'logo'  # 匹配logo文件的关键词,不区分大小写
PPT_PAGE_WIDTH = Inches(13.33)  # 16:9页面宽度
PPT_PAGE_HEIGHT = Inches(7.5)  # 16:9页面高度
LOGO_MAX_WIDTH = Inches(1.5)  # logo最大宽度
LOGO_MARGIN = Inches(0.3)  # logo距离页面右下角边距
OUTPUT_PPT_NAME = 'output.pptx'

def main(target_dir):
    # 校验目录存在
    if not os.path.isdir(target_dir):
        print(f"错误:目录 {target_dir} 不存在")
        return
    
    # 遍历目录获取所有图片和logo
    img_list = []
    logo_path = None
    for filename in os.listdir(target_dir):
        file_path = os.path.join(target_dir, filename)
        if not os.path.isfile(file_path):
            continue
        ext = os.path.splitext(filename)[1].lower()
        if ext not in SUPPORTED_IMG_EXTS:
            continue
        # 判断是否是logo
        if LOGO_KEYWORD in filename.lower():
            logo_path = file_path
            continue
        img_list.append(file_path)
    
    if not img_list:
        print(f"提示:目录 {target_dir} 下未找到符合要求的图片文件")
        return
    
    # 初始化PPT
    prs = Presentation()
    prs.slide_width = PPT_PAGE_WIDTH
    prs.slide_height = PPT_PAGE_HEIGHT
    blank_slide_layout = prs.slide_layouts[6]  # 空白页面布局

    for img_path in img_list:
        slide = prs.slides.add_slide(blank_slide_layout)
        # 计算图片缩放比例,铺满页面不拉伸
        with Image.open(img_path) as img:
            img_w, img_h = img.size
            scale = min(PPT_PAGE_WIDTH / img_w, PPT_PAGE_HEIGHT / img_h)
            display_w = img_w * scale
            display_h = img_h * scale
            # 图片居中
            left = (PPT_PAGE_WIDTH - display_w) / 2
            top = (PPT_PAGE_HEIGHT - display_h) / 2
        # 添加图片到PPT
        slide.shapes.add_picture(img_path, left, top, width=display_w, height=display_h)

        # 添加logo
        if logo_path:
            with Image.open(logo_path) as logo_img:
                logo_w, logo_h = logo_img.size
                logo_scale = LOGO_MAX_WIDTH / logo_w
                logo_display_w = logo_w * logo_scale
                logo_display_h = logo_h * logo_scale
                # 右下角位置
                logo_left = PPT_PAGE_WIDTH - logo_display_w - LOGO_MARGIN
                logo_top = PPT_PAGE_HEIGHT - logo_display_h - LOGO_MARGIN
            slide.shapes.add_picture(logo_path, logo_left, logo_top, width=logo_display_w, height=logo_display_h)
    
    # 保存PPT到指定目录
    output_path = os.path.join(target_dir, OUTPUT_PPT_NAME)
    prs.save(output_path)
    print(f"PPT生成完成,已保存到:{output_path}")

if __name__ == "__main__":
    # 支持传入目录参数,默认使用当前运行目录
    target_dir = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
    main(target_dir)

使用方法

  1. 将上述代码保存为img_to_ppt.py
  2. 运行脚本:
    • 处理当前目录:python img_to_ppt.py
    • 处理指定目录:python img_to_ppt.py 你的图片目录绝对路径
  3. 生成的output.pptx会保存在你指定的图片目录下。

内容的提问来源于stack exchange,提问作者Jitender Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 04:15:01