使用Python将目录中的JPG图片按后缀数字从小到大顺序纵向拼接,很适合老师发的零散图片拼接一个图片
### pip install Pillow -i https://mirror.sjtu.edu.cn/pypi/web/simple
import os
import re
from PIL import Image
def extract_number(filename):
"""从文件名中提取数字部分"""
match = re.search(r'\d+', filename)
return int(match.group()) if match else -1
def combine_images_with_pillow(input_dir, output_path):
"""使用Pillow将目录中的JPG图片按后缀数字从小到大顺序纵向拼接"""
# 获取目录中所有jpg文件
jpg_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.jpg')]
if not jpg_files:
print(f"在目录 {input_dir} 中未找到jpg文件")
return
# 按数字后缀排序
jpg_files.sort(key=extract_number)
# 排除输出文件
output_filename = os.path.basename(output_path)
jpg_files = [f for f in jpg_files if f != output_filename]
if not jpg_