本文目录导读:

可以的,脚本可以轻松实现多张图片自动拼接为长图的功能,我为您提供几种常见的方法:
使用 Python(PIL/Pillow 库)
from PIL import Image
import os
import glob
def images_to_long_image(image_folder, output_path="long_image.png"):
"""
将文件夹中的图片垂直拼接成长图
"""
# 获取所有图片文件(支持常见格式)
image_files = glob.glob(os.path.join(image_folder, "*.[pj][np]g")) + \
glob.glob(os.path.join(image_folder, "*.[pj][np][eg]"))
if not image_files:
print("没有找到图片文件")
return
# 按文件名排序
image_files.sort()
# 打开所有图片
images = [Image.open(img) for img in image_files]
# 计算总高度和最大宽度
total_height = sum(img.height for img in images)
max_width = max(img.width for img in images)
# 创建新图片
long_image = Image.new('RGB', (max_width, total_height), 'white')
# 拼接图片
y_offset = 0
for img in images:
# 如果需要,可以居中图片
x_offset = (max_width - img.width) // 2
long_image.paste(img, (x_offset, y_offset))
y_offset += img.height
# 保存
long_image.save(output_path)
print(f"长图已保存至: {output_path}")
# 使用方法
images_to_long_image("你的图片文件夹路径")
使用 ImageMagick(命令行)
# 垂直拼接 convert image1.jpg image2.jpg image3.jpg -append long_image.jpg # 水平拼接 convert image1.jpg image2.jpg image3.jpg +append long_image.jpg
或批量处理:
# 将所有jpg图片拼接 convert *.jpg -append long_image.jpg
使用 FFmpeg(支持视频帧转图片拼接)
# 从视频提取帧并拼接 ffmpeg -i input.mp4 -vf "select=not(mod(n\,30)),scale=1920:-1" -frames:v 10 frame_%03d.jpg # 然后使用上面的方法拼接 convert frame_*.jpg -append long_image.jpg
简易的 Bash 脚本
#!/bin/bash
# 图片拼接脚本
if [ $# -lt 1 ]; then
echo "用法: $0 <图片文件夹路径>"
exit 1
fi
# 设置输出文件名字
OUTPUT="long_image_$(date +%Y%m%d_%H%M%S).jpg"
# 使用 ImageMagick 拼接
convert "$1"/*.jpg "$1"/*.png -append "$OUTPUT"
echo "长图已生成: $OUTPUT"
注意事项:
-
安装依赖:
- Python方法:
pip install Pillow - ImageMagick方法:
apt-get install imagemagick或brew install imagemagick
- Python方法:
-
图片格式:支持 JPG、PNG、BMP 等常见格式
-
排序问题:如果需要特定顺序,记得先对文件排序
-
内存考虑:大量大图片可能会消耗较多内存
您可以根据具体需求选择适合的方法,如果需要更复杂的处理(如自动调整大小、添加间距等),我可以提供相应的代码改进。