合并图片成长图
是什么:将多张图片垂直拼接为单张长图片。
为什么:方便查看连续内容,适合聊天分享和网页展示。
Python 示例
python
from PIL import Image
import os
def merge_images(image_folder, output_path="long_image.jpg"):
"""合并文件夹中的图片为长图"""
# 获取所有图片文件
images = []
for file in sorted(os.listdir(image_folder)):
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
img_path = os.path.join(image_folder, file)
images.append(Image.open(img_path))
# 计算总尺寸
total_height = sum(img.height for img in images)
max_width = max(img.width for img in images)
# 创建长图并拼接
long_img = Image.new('RGB', (max_width, total_height), 'white')
y_offset = 0
for img in images:
long_img.paste(img, (0, y_offset))
y_offset += img.height
# 保存长图
long_img.save(output_path, 'JPEG', quality=90)
print(f"合并完成: {output_path} ({len(images)}张图片)")
# 使用示例
merge_images("images", "merged_long_image.jpg")
安装依赖:
bash
pip install Pillow
一句话总结:将多张图片垂直拼接为单张长图,自动处理尺寸对齐,适合制作聊天记录和内容展示。