Linux 下 GIMP 自动化处理的实用方案
一 方案总览
二 命令行无头批处理
#!/usr/bin/env bash
for f in *.jpg; do
[ -e "$f" ] || continue
gimp -i -b "(let* ((img (car (gimp-file-load RUN-NONINTERACTIVE \"$f\" \"$f\")))
(lay (car (gimp-image-get-active-layer img))))
(gimp-image-scale img
(* (car (gimp-image-width img)) 0.5)
(* (car (gimp-image-height img)) 0.5))
(gimp-file-save RUN-NONINTERACTIVE img lay
\"${f%.jpg}_scaled.jpg\" \"${f%.jpg}_scaled.jpg\")
(gimp-image-delete img))" \
-b "(gimp-quit 0)"
done
#!/usr/bin/env python
from gimpfu import *
import os
def batch_resize(input_dir, output_dir, w, h):
os.makedirs(output_dir, exist_ok=True)
for fn in os.listdir(input_dir):
if not fn.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
in_path = os.path.join(input_dir, fn)
out_path = os.path.join(output_dir,
f"{os.path.splitext(fn)[0]}_resized{os.path.splitext(fn)[1]}")
img = pdb.gimp_file_load(in_path, in_path)
lay = pdb.gimp_image_get_active_layer(img)
pdb.gimp_image_scale(img, w, h)
pdb.gimp_file_save(img, lay, out_path, out_path)
pdb.gimp_image_delete(img)
register(
"python_fu_batch_resize",
"Batch Resize Images",
"Resize images in a folder",
"Your Name", "Your Name", "2025",
"", "", # 无图像菜单入口,纯脚本调用
[
(PF_DIRNAME, "input_dir", "Input Directory", ""),
(PF_DIRNAME, "output_dir", "Output Directory", ""),
(PF_INT, "width", "Target Width", 800),
(PF_INT, "height", "Target Height", 600),
],
[],
batch_resize
)
main()
保存为 batch_resize.py,复制到 ~/.config/GIMP/2.10/plug-ins/,赋权后在无头环境运行:gimp -i -b '(python-fu-batch-resize RUN-NONINTERACTIVE "/path/in" "/path/out" 1280 720)' -b '(gimp-quit 0)'
三 图形化批量插件 BIMP
四 脚本语言与目录速查
五 性能替代与建议
convert *.jpg -resize 50% output_%d.jpg
sudo apt-get install imagemagick # Debian/Ubuntu
sudo dnf install imagemagick # Fedora/CentOS