Run GIMP Batch Mode from Linux Terminal

This article explains how to execute the GNU Image Manipulation Program (GIMP) in a non-interactive batch mode directly from a Linux bash shell. By running GIMP without its graphical user interface, you can automate repetitive image editing tasks, such as resizing, converting formats, or applying filters. The following sections break down the essential command-line arguments, provide syntax for Script-Fu commands, and demonstrate how to process images automatically without manual user intervention.

Essential Command-Line Flags

To run GIMP without launching the user interface, you must use specific command-line switches in your terminal:

  • -i or --no-interface: Runs GIMP without the graphical user interface (GUI).
  • -b or --batch: Specifies the command to execute. Multiple -b commands can be chained together and are processed in order.
  • --batch-interpreter: Specifies which interpreter should handle the batch commands (defaults to Script-Fu/TinyScheme).
  • -q or --no-splash: Disables the startup splash screen and extra console messages.

The Basic Batch Command Structure

GIMP processes batch commands using TinyScheme (Script-Fu) by default. A critical requirement when running in batch mode is including the (gimp-quit 0) command as the final batch instruction; otherwise, the GIMP process will remain hanging in the terminal indefinitely.

The standard execution structure looks like this:

gimp -i -b '(batch-command-here)' -b '(gimp-quit 0)'

Inline Script-Fu Example: Inverting an Image

You can execute Procedural Database (PDB) calls directly from bash by enclosing the Scheme code inside single quotes:

gimp -i -b '
(let* (
    (image (car (gimp-file-load RUN-NONINTERACTIVE "input.png" "input.png")))
    (drawable (car (gimp-image-get-active-layer image)))
  )
  (gimp-drawable-invert drawable FALSE)
  (gimp-file-save RUN-NONINTERACTIVE image drawable "output.png" "output.png")
  (gimp-image-delete image)
)' -b '(gimp-quit 0)'

Running an External Script-Fu File

Writing long scripts directly into the terminal can become difficult to maintain. Instead, you can define a Script-Fu function inside a .scm file and save it in your GIMP scripts directory (typically ~/.config/GIMP/2.10/scripts/).

  1. Define a function in ~/.config/GIMP/2.10/scripts/batch-resize.scm:
(define (batch-resize pattern width height)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
      (let* ((filename (car filelist))
             (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
             (drawable (car (gimp-image-get-active-layer image))))
        (gimp-image-scale-full image width height INTERPOLATION-CUBIC)
        (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
        (gimp-image-delete image))
      (set! filelist (cdr filelist)))))
  1. Call your function from the bash terminal:
gimp -i -b '(batch-resize "*.jpg" 800 600)' -b '(gimp-quit 0)'

Using Python-Fu for Batch Execution

If your GIMP installation supports Python-Fu, you can use the Python interpreter instead of Script-Fu by specifying --batch-interpreter python-fu-eval:

gimp -i --batch-interpreter python-fu-eval -b '
import gimpfu

image = pdb.gimp_file_load("input.jpg", "input.jpg")
drawable = pdb.gimp_image_get_active_layer(image)
pdb.gimp_drawable_invert(drawable, False)
pdb.gimp_file_save(image, drawable, "output.jpg", "output.jpg")
pdb.gimp_image_delete(image)
' -b 'pdb.gimp_quit(0)'