O que você vai aprender
- Processar imagens em lote via linha de comando sem interface gráfica
- Integrar o GIMP em pipelines de automação (Makefiles, CI/CD)
- Usar gimp -i -b para conversão em massa e aplicação de filtros repetitivos
Definição🔗
O modo batch permite executar o GIMP sem interface gráfica, processando imagens a partir de scripts (Script-Fu ou Python-Fu) invocados pela linha de comando. É o modo ideal para conversão em massa, aplicação de pipelines de filtros, geração de thumbnails e qualquer tarefa repetitiva sobre centenas ou milhares de arquivos. O processo é totalmente não-interativo: nenhum diálogo aparece, e o GIMP encerra após (gimp-quit 0).
Use modo batch quando: precisa processar diretórios inteiros; quer integrar o GIMP a pipelines CI/CD ou Makefiles; ou não tem acesso a um display X11/Wayland (servidores headless).
Conceitos🔗
flowchart LR
A[Shell] -->|gimp -i -b 'script'| B[Binário GIMP]
B --> C{Carregar PDB}
C --> D[Executa Script-Fu / Python-Fu]
D --> E[Chamadas PDB]
E --> F[Processamento de imagens]
F --> G[gimp-quit 0]
G --> H[Exit code]
Conceitos-chave:
- Flag
-i—--no-interfaceou--no-data. Inicia o GIMP sem GUI, sem carregar ícones ou fontes, ideal para batch. - Flag
-b—--batch. Executa um comando Script-Fu (ou Python-Fu) passado como argumento. RUN-NONINTERACTIVE— constante passada para procedimentos que normalmente abririam diálogo. Obrigatória em batch; senão o procedimento trava esperando input do usuário.gimp-quit 0— encerra o GIMP programaticamente. O argumento0é o exit code. Esquecer de chamar deixa o processo pendurado.- Stdin vs argumento — em scripts longos, prefira passar via heredoc (
<<'EOF') ou-b - <<EOF. Em scripts curtos,-b '(comando)'direto.
Fluxo de Uso🔗
sequenceDiagram
participant Shell
participant GIMP as GIMP batch
participant PDB
participant FS as Filesystem
Shell->>GIMP: gimp -i -b '(script)'
GIMP->>PDB: Carregar catálogo
PDB-->>GIMP: Procedimentos disponíveis
loop Para cada arquivo
GIMP->>FS: Ler imagem
FS-->>GIMP: bytes
GIMP->>PDB: Aplicar filtros
PDB->>FS: Escrever saída
end
GIMP->>Shell: gimp-quit 0
Shell->>Shell: exit 0Exemplos Práticos🔗
Exemplo 1 — Converter uma imagem PNG para JPG
gimp -i -b '(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE "entrada.png" "entrada.png")))
(drawable (car (gimp-image-get-active-drawable image))))
(file-jpeg-save RUN-NONINTERACTIVE image drawable "saida.jpg" "saida.jpg" 0.9 0 0 0 "" 0 0 0 0)
(gimp-image-delete image)
(gimp-quit 0))'
-i desabilita GUI; -b executa o script; RUN-NONINTERACTIVE impede qualquer diálogo; gimp-quit 0 finaliza com sucesso.
Exemplo 2 — Redimensionar todas as JPG de um diretório
gimp -i -b '
(let* ((files (cadr (file-glob "/tmp/imgs/*.jpg" 1)))
(filename ""))
(while (not (null? files))
(set! filename (car files))
(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
(drawable (car (gimp-image-get-active-drawable image))))
(gimp-image-scale-full image 800 600 INTERPOLATION-CUBIC)
(file-jpeg-save RUN-NONINTERACTIVE image drawable filename filename 0.85 0 0 0 "" 0 0 0 0)
(gimp-image-delete image))
(set! files (cdr files)))
(gimp-quit 0))'
O loop itera via file-glob (que retorna (contador . lista)), aplica redimensionamento e sobrescreve cada JPG com qualidade 85.
Exemplo 3 — Converter PNG para WebP em lote (Python)
gimp -i -b '
(python-fu-eval 0 "from gimpfu import *
import glob, os
for f in glob.glob(\"/tmp/imgs/*.png\"):
img = pdb.gimp_file_load(f, f)
image = img[1] if isinstance(img, tuple) else img[0]
drawable = pdb.gimp_image_get_active_drawable(image)
pdb.file_webp_save(image, drawable, f.replace(\".png\", \".webp\"), f.replace(\".png\", \".webp\"), 0, 9, 0)
pdb.gimp_image_delete(image)
print(\"done\")")
(gimp-quit 0)'
python-fu-eval permite executar código Python arbitrário a partir do shell. Aqui o código lida com o retorno em tupla do GIMP 3.
Exemplo 4 — Aplicar marca d'água em todas as imagens
gimp -i -b '
(let* ((logo (car (gimp-file-load RUN-NONINTERACTIVE "/opt/logo.png" "/opt/logo.png")))
(logo-drawable (car (gimp-image-get-active-drawable logo)))
(files (cadr (file-glob "/tmp/fotos/*.jpg" 1))))
(gimp-image-set-active-layer logo logo-drawable)
(gimp-selection-all logo)
(gimp-edit-copy logo-drawable)
(gimp-image-delete logo)
(while (not (null? files))
(let* ((f (car files))
(image (car (gimp-file-load RUN-NONINTERACTIVE f f)))
(drawable (car (gimp-image-get-active-drawable image)))
(floating (car (gimp-edit-paste drawable FALSE))))
(gimp-layer-set-opacity floating 50.0)
(gimp-layer-set-offsets floating 20 20)
(gimp-floating-sel-anchor floating)
(gimp-image-flatten image)
(file-jpeg-save RUN-NONINTERACTIVE image drawable f f 0.9 0 0 0 "" 0 0 0 0)
(gimp-image-delete image))
(set! files (cdr files)))
(gimp-quit 0))'
Carrega a logo uma única vez, copia para a clipboard interna, depois cola em cada foto com opacidade 50% no canto inferior esquerdo.
Exemplo 5 — Pipeline completo via heredoc
Para scripts longos, use heredoc para evitar problemas com aspas:
gimp -i -b - <<'EOF'
(let* ((src-dir "/tmp/entrada/")
(dst-dir "/tmp/saida/")
(files (cadr (file-glob (string-append src-dir "*.png") 1))))
(while (not (null? files))
(let* ((src (car files))
(dst (string-append dst-dir (car (last (strsplit src "/"))))))
(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE src src)))
(drawable (car (gimp-image-get-active-drawable image))))
(gimp-drawable-desaturate drawable DESATURATE-LUMINOSITY)
(plug-in-gauss RUN-NONINTERACTIVE image drawable 1.5 1.5 0)
(gimp-image-scale-full image 1024 768 INTERPOLATION-CUBIC)
(file-png-save RUN-NONINTERACTIVE image drawable dst dst 0 9 1 1 1 1 1)
(gimp-image-delete image)))
(set! files (cdr files)))
(gimp-quit 0))
EOF
As aspas em <<'EOF' (com aspas simples) impedem expansão de variáveis pelo shell; o conteúdo vai literal para o GIMP.
Opções de Linha de Comando🔗
| Flag | Significado |
|---|---|
-i / --no-interface | Inicia sem GUI |
-b [comando] / --batch [comando] | Executa comando Script-Fu |
-d / --no-data | Não carrega patterns, gradientes, brushes |
--console-messages | Exibe mensagens do console no stderr |
--verbose | Saída detalhada para debug |
- | Le comandos do stdin (use com -b -) |
Quando Usar Batch🔗
| Cenário | Modo batch | Console interativo |
|---|---|---|
| Processar 1000 fotos | sim | não |
| Testar um filtro | não | sim |
| Servidor CI/CD sem display | sim | não |
| Aprender a API | não | sim |
| Pipeline determinístico | sim | não |
Você aprendeu
- O modo batch executa o GIMP sem interface gráfica via
gimp -i -b 'script' - Ideal para processar diretórios inteiros, gerar thumbnails e integrar em pipelines CI/CD
- Funciona em servidores headless sem display X11/Wayland