https://github.com/theotherphil/imagecli 需要你自己编译到 Linux 版本,再配合 python 很容易满足你的要求
```python
import os
import subprocess
import argparse
from PIL import Image
import re
if __name__ == "__main__":
# python
jpeg_hcat.py title a.jpg b.jpg c.jpg --p="hcat"
# 接收所有输入的参数并打印
# print(sys.argv[1:])
# 创建 ArgumentParser 对象
parser = argparse.ArgumentParser(description="示例程序")
# 添加 --type 参数
parser.add_argument(
"--type",
type=str,
default="hcat",
choices=["hcat", "vcat", "grid"],
help="操作类型",
)
# 添加 --cols 参数
parser.add_argument(
"--cols",
type=int,
default=3,
help="列数",
)
# 添加 --name 参数
parser.add_argument(
"--name",
type=str,
default="image",
help="输出文件名",
)
# 添加 --cols 参数
parser.add_argument(
"--author",
type=str,
default="",
help="输出作者",
)
parser.add_argument("files", nargs="+", type=str, help="输入的文件列表")
# 解析命令行参数
args = parser.parse_args()
process = args.type
files = []
name =
args.name # 接收所有输入的参数并检查参数是否包含文件
for file in args.files:
# 检查文件是否存在
temp = file.lower()
if os.path.exists(file) and os.path.isfile(file):
if temp.endswith(".jpg") or temp.endswith(".jpeg") or temp.endswith(".png"):
# 将文件名转为小写后判断
files.append(file)
# 文件数量
file_len = len(files)
if file_len == 0:
print("没有文件")
exit(0)
renames = []
image = Image.open(files[0])
width, height = image.size
rows = file_len
if process == "grid":
if rows < args.cols:
process = "hcat"
else:
# 计算行数
rows_sqrt = int(rows**0.5)
cols = rows_sqrt
if rows_sqrt > 5:
cols = 5
rows = file_len // 5
else:
cols = rows_sqrt
rows = rows_sqrt
if cols * rows < file_len:
for i in [4, 3, 2]:
n = file_len // i
if n * i == file_len:
rows = n
cols = i
break
process = f"grid {cols}"
pattern = r"(\d)x(\d)"
matrix = re.findall(pattern, args.author)
if matrix and len(matrix) == 1:
cols = int(matrix[0][0])
rows = int(matrix[0][1])
miss = cols * rows - file_len
if miss > 0:
# 补齐
id = ", ".join(["id"] * file_len)
process = f"[{id}, (new {width} {height} (255, 255, 255) > DUP {miss-1})] > grid {cols}"
# 获取文件拓展名
ext = os.path.splitext(files[0])[1]
# 使用第一文件路径作为输出文件路径,并使用 name 作为文件名
name = name + ext
output = os.path.join(os.path.dirname(files[0]), name)
target = f'"{output}"'
# 将 files 格式化为字符串,以空格分隔,使用引号将文件名括起来
quoted_files = ['"{}"'.format(file) for file in files]
input = " ".join(quoted_files)
# 运行命令
cmd = f'imagecli.exe -i {input} -o {target} -p "{process} {rows}"'
# os.system(cmd)
result =
subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
# 打开图片
# 1. 检查图片是否存在
if os.path.exists(output):
# 2. 打开图片
os.startfile(output)
# 接收所有输入的参数并保存到指定文件中
out = "D:\Program Files\clawPDF\scripts\log.txt"
with open(out, "w") as f:
f.write(cmd + "\n")
```