> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nika.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Image FX batch

> T1 fan-out · creators / media: a folder of photos to deterministic art on the same pinned engine. Zero model, zero network.

> **T1 fan-out · creators / media**: `nika:glob` lists the folder,
> `for_each` fans one `nika:image_fx` task per photo, and a fixed ops
> chain (grayscale → dither → pixelate) renders each one. No model in
> the loop — the pipeline is pure pixels, so the same input produces
> the same bytes on every run, on every machine.

## The job

Batch-stylizing a shoot means opening an editor, replaying the same
five clicks per photo, and hoping you replayed them identically. This
workflow IS the preset: the ops chain is written once in YAML, the
batch fans out in parallel, and re-running it on the **same pinned
engine** with the same input and ops yields the same bytes. An engine
upgrade is a new pin — re-render before treating an old hash as
current.

## The shape

```mermaid theme={"system"}
flowchart LR
  shots["shots · nika:glob"]:::invoke
  jobs["jobs · nika:jq"]:::invoke
  stylize["stylize · ∥ for_each · nika:image_fx"]:::invoke
  shots --> jobs
  jobs --> stylize
  classDef invoke fill:#22d3ee22,stroke:#22d3ee,color:#22d3ee
```

## The file

```yaml image-fx-batch.nika theme={"system"}
nika: image-fx-batch

const:
  # Two sample photos ship with the repo. Point this at your own folder and
  # move `permits.fs.read` with it — a const is baked in, never caller-supplied,
  # so the two lines stay in step by editing, not by magic.
  photos: "examples/fixtures/photos"

permits:
  exec: false
  tools: ["nika:glob", "nika:image_fx", "nika:jq"]
  fs:
    # ONE entry that has to cover TWO different reads, and this is the trap:
    #   · `nika:glob` gates its WALK ROOT — the directory itself
    #   · `nika:image_fx` gates each MATCHED FILE underneath it
    # Measured against 0.106.0: granting only `…/photos` passes `check` with
    # zero hints and then dies at run — NIKA-SEC-004 on the first photo.
    # Granting only `…/photos/*.png` is worse: `check` calls the entry drift
    # (it matches no literal the body names) and the glob is refused at run.
    # `**` covers the directory AND every descendant, so one entry is honest
    # here; `["…/photos", "…/photos/*.png"]` is the two-entry equivalent.
    read: ["examples/fixtures/photos/**"]
    # The renders land FLAT in one directory, so `*` is enough: it matches a
    # single path segment and never crosses `/`, which means a photo whose
    # name contained a slash could not steer a write into a subtree.
    write: ["out/darkroom/*"]

tasks:
  # The folder is the work list — no hand-maintained array to drift.
  shots:
    invoke:
      tool: "nika:glob"
      args: { pattern: "${{ const.photos }}/*.png" }

  # Derive {src, out} pairs · jq does the string work, natively.
  jobs:
    with:
      shots: ${{ tasks.shots.output }}
    invoke:
      tool: "nika:jq"
      args:
        input: "${{ with.shots }}"
        expression: 'map({ src: ., out: ("out/darkroom/" + (split("/") | last)) })'

  # One deterministic darkroom pass per photo · the ops run IN ORDER.
  stylize:
    with:
      jobs: ${{ tasks.jobs.output }}
    for_each:
      items: ${{ with.jobs }}
      max_parallel: 4
    invoke:
      tool: "nika:image_fx"
      args:
        input: "${{ item.src }}"
        out: "${{ item.out }}"
        seed: 0                       # explicit · the render is a function of (bytes · ops · seed)
        ops:
          - grayscale: {}
          - dither: { mode: bayer4 }
          - pixelate: { block: 4 }

outputs:
  rendered:
    # an empty folder skips the fan-out (its output is null) — the guard keeps
    # the typed contract honest on the very first exploratory run
    value: "${{ tasks.stylize.output != null ? tasks.stylize.output : [] }}"
    description: "The stylized artifact receipts · one per input photo · path + sha256"
```

## Two things this file teaches

1. **The ops vocabulary is a closed set** — `dither: { mode: bayer4 }`
   is one of the engine's named modes; misspell it (`bayer`) and
   `nika check` answers with the whole legal set instead of guessing.
   The checker teaches the vocabulary before the run spends a cycle.
2. **A fan-out that can produce nothing still binds** — the `rendered`
   output guards with `${{ tasks.stylize.output != null ? … : [] }}`
   (quoted, because a bare CEL ternary contains `:` and YAML would
   claim it). An empty folder degrades to an empty list, not a crash.

## Run it

> [!NOTE]
>
> **What it needs:** The two sample PNG files. No model, no key, no network.

**1. Rehearse it**

```bash theme={"system"}
nika try image-fx-batch
```

The sample files come with it and nothing is written in your folder.

**2. Get your own copy**

Save the file above as `image-fx-batch.nika` in a new folder, then download the samples it reads:

```bash theme={"system"}
FIXTURES=https://raw.githubusercontent.com/supernovae-st/nika/v0.124.1/crates/nika-pack/pack/examples/fixtures
mkdir -p examples/fixtures/photos
curl -fsSLo examples/fixtures/photos/sunset-01.png "$FIXTURES/photos/sunset-01.png"
curl -fsSLo examples/fixtures/photos/harbour-02.png "$FIXTURES/photos/harbour-02.png"
```

**3. Check it, then run it**

```bash theme={"system"}
nika check image-fx-batch.nika
nika run image-fx-batch.nika
```

The run writes one stylized PNG per photo under `out/darkroom/`.

To stylize your own folder, edit `const.photos` and the matching `permits.fs.read` entry together: a permit is a literal and cannot follow the constant (`NIKA-AUTH-007`), and a folder outside it is refused at run (`NIKA-SEC-004`).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.