本文目录导读:

我可以为你提供几种常用的图片拼接脚本方案,涵盖不同编程语言和需求场景。
Python + Pillow(最常用)
from PIL import Image
import os
def concat_images_horizontal(image_paths, output_path):
"""水平拼接图片"""
images = [Image.open(path) for path in image_paths]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_img = Image.new('RGB', (total_width, max_height))
x_offset = 0
for img in images:
new_img.paste(img, (x_offset, 0))
x_offset += img.width
new_img.save(output_path)
print(f"拼接完成: {output_path}")
def concat_images_vertical(image_paths, output_path):
"""垂直拼接图片"""
images = [Image.open(path) for path in image_paths]
widths, heights = zip(*(i.size for i in images))
max_width = max(widths)
total_height = sum(heights)
new_img = Image.new('RGB', (max_width, total_height))
y_offset = 0
for img in images:
new_img.paste(img, (0, y_offset))
y_offset += img.height
new_img.save(output_path)
print(f"拼接完成: {output_path}")
# 使用示例
image_list = ['image1.jpg', 'image2.jpg', 'image3.jpg']
concat_images_horizontal(image_list, 'output_horizontal.jpg')
concat_images_vertical(image_list, 'output_vertical.jpg')
Python + OpenCV(需要统一尺寸)
import cv2
import numpy as np
import os
def concat_with_opencv(image_paths, output_path, direction='horizontal'):
"""使用OpenCV拼接图片"""
images = []
for path in image_paths:
img = cv2.imread(path)
if img is not None:
# 统一尺寸(可选)
img = cv2.resize(img, (500, 500))
images.append(img)
if not images:
print("没有有效的图片")
return
if direction == 'horizontal':
result = np.hstack(images) # 水平拼接
else:
result = np.vstack(images) # 垂直拼接
cv2.imwrite(output_path, result)
print(f"拼接完成: {output_path}")
# 使用示例
image_list = ['img1.jpg', 'img2.jpg', 'img3.jpg']
concat_with_opencv(image_list, 'opencv_result.jpg', 'horizontal')
命令行工具 ImageMagick
# 水平拼接 convert image1.jpg image2.jpg image3.jpg +append output.jpg # 垂直拼接 convert image1.jpg image2.jpg image3.jpg -append output.jpg # 使用通配符 convert *.jpg +append 全部拼接.jpg # 添加间距 convert image1.jpg image2.jpg -gravity center -background white -resize 800x +append output.jpg
Bash + ffmpeg
#!/bin/bash
# 水平拼接
ffmpeg -i image1.jpg -i image2.jpg -i image3.jpg -filter_complex "[0:v][1:v][2:v]hstack=inputs=3" output.jpg
# 垂直拼接
ffmpeg -i image1.jpg -i image2.jpg -filter_complex "[0:v][1:v]vstack=inputs=2" output.jpg
# 2x2网格拼接
ffmpeg -i img1.jpg -i img2.jpg -i img3.jpg -i img4.jpg \
-filter_complex "[0:v][1:v]hstack=inputs=2[top];[2:v][3:v]hstack=inputs=2[bottom];[top][bottom]vstack=inputs=2" \
output.jpg
完整批量处理脚本(Python)
from PIL import Image
import os
import glob
def batch_concat(folder_path, prefix='img', output='combined'):
"""
批量拼接文件夹内所有图片
"""
# 获取所有图片
patterns = ['*.jpg', '*.jpeg', '*.png', '*.bmp']
image_files = []
for pattern in patterns:
image_files.extend(glob.glob(os.path.join(folder_path, pattern)))
if not image_files:
print("没有找到图片文件")
return
image_files.sort()
print(f"找到 {len(image_files)} 张图片")
# 打开所有图片
images = [Image.open(f) for f in image_files]
# 水平拼接
total_width = sum(img.width for img in images)
max_height = max(img.height for img in images)
combined = Image.new('RGB', (total_width, max_height), 'white')
x_offset = 0
for img in images:
combined.paste(img, (x_offset, 0))
x_offset += img.width
# 保存结果
output_path = os.path.join(folder_path, f'{output}.jpg')
combined.save(output_path, quality=95)
print(f"拼接完成,结果保存在: {output_path}")
# 使用示例
batch_concat('/path/to/images/folder', output='all_images')
使用建议
- Pillow方法:适合大多数场景,不需要额外安装
- OpenCV方法:处理速度更快,适合大量图片
- ImageMagick:命令行方便,功能强大
- ffmpeg:适合视频帧或需要复杂滤镜的场景
如果你想要更高级的功能(如自动调整大小、添加水印、网格拼接等),我可以提供更详细的实现,你想用在什么场景?