linux

Linux GIMP如何进行批量图片处理

小樊
48
2025-08-04 23:05:49
栏目: 智能运维

在Linux系统上使用GIMP进行批量图片处理可以通过多种方法实现,以下是一些常见的方法:

使用Shell脚本

你可以编写一个Shell脚本来自动化GIMP的一些重复任务。例如,创建一个脚本文件gimp_batch.sh,内容如下:

#!/bin/bash
# 设置输入和输出文件夹
INPUT_FOLDER="/path/to/input/folder"
OUTPUT_FOLDER="/path/to/output/folder"
# 遍历输入文件夹中的所有图片文件
for file in "$INPUT_FOLDER"/*.{jpg,png,bmp}
do
  # 打开图片
  gimp "$file"
  
  # 进行批量处理操作,例如调整大小、添加水印等
  # 示例:调整所有图片大小为800x600像素
  gimp --batch-mode --resize=800,600 "$file"
  
  # 保存处理后的图片到输出文件夹
  mv "$file" "$OUTPUT_FOLDER"
done

然后给脚本执行权限:

chmod +x gimp_batch.sh

运行批处理脚本:

./gimp_batch.sh

使用GIMP的Python-Fu脚本

GIMP的Python-Fu脚本提供了更强大的批量处理能力。以下是一个示例,展示如何使用Python-Fu脚本批量调整图片大小:

import os
from PIL import Image

def batch_resize_images(input_directory, output_directory, size):
    if not os.path.exists(output_directory):
        os.makedirs(output_directory)
    
    for filename in os.listdir(input_directory):
        if filename.endswith(('.png', '.jpg', '.jpeg')):
            file_path = os.path.join(input_directory, filename)
            img = Image.open(file_path)
            img_resized = img.resize(size, Image.ANTIALIAS)
            output_filename = os.path.splitext(filename)[0] + "_resized." + os.path.splitext(filename)[1]
            output_path = os.path.join(output_directory, output_filename)
            img_resized.save(output_path)
            print(f"Resized {file_path} to {output_path}")

# 定义输入和输出目录
input_directory = "."
output_directory = "./resized_images"
size = (800, 600)  # 指定目标大小
# 调用批量调整图片大小的函数
batch_resize_images(input_directory, output_directory, size)

将上述脚本保存为batch_resize.py,然后使用GIMP运行脚本:

gimp -i batch_resize.py

使用GIMP插件

GIMP支持插件,你可以编写或下载插件来扩展其功能。例如,使用BatchResize插件可以帮助你批量调整图像大小。

  1. 安装插件:
sudo apt-get install gimp-plugin-batchresize
  1. 加载插件:
  1. 使用插件:

通过这些方法,你可以在Linux上使用GIMP进行批处理操作,提高工作效率。

0
看了该问题的人还看了