在 Debian 上使用 GIMP 进行批量处理
方法一 命令行批处理脚本 GIMP Console + Script-Fu
#!/usr/bin/env bash
set -e
shopt -s nullglob
INPUT_DIR="${1:-.}"
OUT_DIR="${2:-./png_out}"
mkdir -p "$OUT_DIR"
for f in "$INPUT_DIR"/*.jpg "$INPUT_DIR"/*.jpeg; do
[ -e "$f" ] || continue
base=$(basename "$f")
out="$OUT_DIR/${base%.*}.png"
echo "Converting: $f -> $out"
gimp-console -i -b "(batch-convert-to png \"$f\" \"$out\")" -b "(gimp-quit 0)"
done
chmod +x batch_convert.sh
./batch_convert.sh /path/to/input /path/to/output
方法二 Python-Fu 插件实现复杂批处理
#!/usr/bin/env python3
from gimpfu import *
import os, sys
def batch_resize_png(input_dir, output_dir, width, height):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for f in os.listdir(input_dir):
if not f.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
in_path = os.path.join(input_dir, f)
out_path = os.path.join(output_dir, f)
try:
img = pdb.gimp_file_load(in_path, in_path)
pdb.gimp_image_scale(img, width, height)
draw = img.active_layer
pdb.file_png_save_defaults(img, draw, out_path, out_path)
pdb.gimp_image_delete(img)
except Exception as e:
pdb.gimp_message("Failed: " + in_path + " -> " + str(e))
register(
"python_fu_batch_resize_png",
"Batch resize images and save as PNG",
"Resize and export as PNG in batch",
"Your Name", "Your Name", "2025",
"<Image>/File/Batch/Resize PNG...", # 菜单路径,可自定义
"*",
[
(PF_DIRNAME, "input_dir", "Input folder", ""),
(PF_DIRNAME, "output_dir", "Output folder", ""),
(PF_INT, "width", "Width", 800),
(PF_INT, "height", "Height", 600),
],
[],
batch_resize_png)
main()
方法三 无编程替代方案与何时选择
convert *.jpg -resize 50% output_%03d.jpg
常见问题与排错要点