在Debian系统中,GIMP支持多种批处理方式,涵盖图形化操作、脚本自动化及Python编程,以下是具体步骤:
确保系统已安装GIMP及Python支持(部分脚本需要):
sudo apt update
sudo apt install gimp python3-gi gir1.2-gimp-3.0 # 安装GIMP及Python绑定
适合无编程基础的用户,通过录制操作步骤实现批量重复处理。
~/.config/GIMP/2.10/actions/)。适合熟悉Shell的用户,通过循环遍历图片并调用GIMP命令处理。
batch_resize.sh,输入以下内容(以调整尺寸为例):#!/bin/bash
# 参数说明:输入目录、输出目录、宽度、高度
input_dir="$1"
output_dir="$2"
width="$3"
height="$4"
mkdir -p "$output_dir" # 创建输出目录
for file in "$input_dir"/*.jpg; do
filename=$(basename "$file")
new_filename="${filename%.*}_resized.${filename##*.}"
new_image="$output_dir/$new_filename"
# 调用GIMP命令行调整尺寸(非交互模式)
gimp -i -b "(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE \"$file\" \"$file\")))
(drawable (car (gimp-image-get-active-layer image))))
(gimp-image-scale image $width $height)
(gimp-file-save RUN-NONINTERACTIVE image drawable \"$new_image\" \"$new_image\")
(gimp-image-delete image))" -b "(gimp-quit 0)"
done
echo "批处理完成!"
chmod +x batch_resize.sh
./batch_resize.sh /path/to/input /path/to/output 800 600
此脚本会将input目录下的所有.jpg图片调整为800x600像素,保存到output目录(文件名添加_resized后缀)。适合需要复杂逻辑(如条件判断、多操作组合)的用户,通过Python调用GIMP的Procedural Database(PDB)。
batch_process.py,输入以下内容(以批量转换为PNG并添加水印为例):import os
from gimpfu import *
def batch_process(input_folder, output_folder):
# 确保输出目录存在
os.makedirs(output_folder, exist_ok=True)
# 遍历输入目录中的图片文件
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, f"processed_{filename}")
# 加载图片
image = pdb.gimp_file_load(input_path, input_path)
drawable = pdb.gimp_image_get_active_layer(image)
# 示例操作:调整尺寸(800x600)
pdb.gimp_image_scale(image, 800, 600)
# 示例操作:添加水印(可选)
# draw_watermark(drawable, "Watermark Text")
# 保存图片(PNG格式,质量95)
pdb.file_png_save(image, drawable, output_path, output_path, 0, 9, 1, 1, 1, 1)
# 关闭图片
pdb.gimp_image_delete(image)
print("批处理完成!")
# 注册脚本(用于GIMP菜单调用)
register(
"python_fu_batch_process",
"批量处理图片(调整尺寸+添加水印)",
"将输入目录的图片调整为800x600并保存到输出目录",
"Your Name", "Your Name", "2025",
"<Toolbox>/Xtns/Languages/Python-Fu/批处理",
"*", # 支持所有图片类型
[
(PF_DIRNAME, "input_folder", "输入目录", ""),
(PF_DIRNAME, "output_folder", "输出目录", "")
],
[],
batch_process)
main()
~/.config/GIMP/2.10/plug-ins/);chmod +x ~/.config/GIMP/2.10/plug-ins/batch_process.py;以上方法覆盖了Debian系统下GIMP批处理的主要场景,用户可根据自身需求选择合适的方式。