---
name: codex-image-gen
description: Generate or edit raster images via Codex CLI and save them to a path. Use for image generation or editing unless another image backend is specified.
license: MIT
title: codex-image-gen
canonical_url: https://skilld.dev/gh/zazencodes/skills/codex-image-gen
last_updated: 2026-11-14T05:01:28.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [agents/openai.yaml](https://skilld.dev/api/skills-raw/zazencodes/skills/codex-image-gen/agents/openai.yaml), [scripts/codex_image.py](https://skilld.dev/api/skills-raw/zazencodes/skills/codex-image-gen/scripts/codex_image.py).
>
> If the user asked to install this Skill, run `npx skilld install zazencodes/skills/codex-image-gen`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/codex-image-gen`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/zazencodes/skills/codex-image-gen) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/codex-image-gen`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/codex-image-gen --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/codex-image-gen --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Image Generation via the codex CLI

## Overview

The `codex` CLI carries its own ChatGPT authentication and ships a built-in image
tool, so any agent on this machine can generate images through it with no
`OPENAI_API_KEY` and no other image service.

`codex exec` has no "write the image to this path" flag: it saves what it
generates to `$CODEX_HOME/generated_images/<session id>/`.
`scripts/codex_image.py` runs one non-interactive turn and collects only images
saved under that run's exact session ID. If the session ID or its saved images
are missing, it fails without writing output.

**Always go through the script.** A hand-rolled `codex exec "draw me a cat"` very
often returns prose or an ImageMagick drawing instead of a generated image, and
leaves you to find the output yourself.

**Never fall back to the OpenAI API.** This skill has no API-key fallback path,
and none should be added or used. Do not call the OpenAI SDK, an `OPENAI_API_KEY`,
or any script that bills a metered OpenAI API account for image generation — not
even when `codex exec` fails or is out of quota. If `codex_image.py` fails, report
the failure (see Failure modes) and stop; do not silently switch to a paid API
path.

## Requirements

- `codex` on `PATH` and logged in (`codex login`). Set `CODEX_BIN` to point at a
  different binary.
- Python 3.9+. Standard library only, except `--fit`, which needs Pillow.
- Image generation consumes the codex account's quota. When it is exhausted the
  script fails with a clear message and writes nothing; report that to the user
  rather than retrying in a loop.

## Quick start

Run from this skill directory, or give the script's absolute path:

```bash
python3 scripts/codex_image.py \
  --prompt "a lone red bicycle leaning against a wet stone wall at dusk, cinematic photograph" \
  --output ~/Desktop/bike.png
```

A run takes roughly 30–60 seconds. On success the script prints each path it
wrote. Output extensions follow the actual image format: PNG bytes requested
as `bike.jpg` are saved as `bike.png`. `--fit` always produces PNG. On
generation or recovery failure it exits non-zero and writes nothing.

## Common invocations

| Goal | Command |
|---|---|
| Basic generation | `codex_image.py -p "<prompt>" -o out.png` |
| Portrait / landscape / square | `codex_image.py -p "..." -o out.png --aspect 9:16` |
| Use a reference image | `codex_image.py -p "..." -o out.png -r ref.png` |
| Edit an existing image | `codex_image.py --edit v1.png -p "make the sky purple" -o v2.png` |
| Several options to choose from | `codex_image.py -p "..." -o opt.png -n 3` |
| Exact pixel dimensions | `codex_image.py -p "..." -o cover.png --aspect 9:16 --fit 1080x1920` |
| Long prompt from a file | `codex_image.py --prompt-file prompt.md -o out.png` |
| Machine-readable result | `codex_image.py -p "..." -o out.png --quiet --json` |

Key flags:

- `--aspect` is a hint passed to the model (`16:9`, `9:16`, `1:1`, `4:5`, …). It
  shapes the generated image but does not guarantee exact dimensions.
- `--fit WxH` guarantees them, after the fact: letterbox-pads by default
  (`--fit-mode pad`, colour via `--fit-background`) or `--fit-mode crop` to
  center-crop instead. Pair it with a matching `--aspect` so there is little to
  pad or crop.
- `-n/--variants N` asks for N takes in one run. The first lands at `-o`, the
  rest beside it as `<name>-2.png`, `<name>-3.png`. The model does not always
  honour the count; the script saves whatever it actually produced.
- `--edit` attaches the image as the edit target and instructs the model to
  change only what the prompt asks for. Write the prompt as the *change*
  ("remove the lamp post"), not as a fresh description of the whole picture.
- `--sandbox` defaults to `read-only`, which is all image generation needs. Use
  `--sandbox bypass` only where the nested codex sandbox cannot start at all
  (a container without user namespaces, for instance).
- `--reasoning` defaults to `high` because lower effort makes the model skip the
  image tool and answer in text.
- `--print-prompt` shows the fully assembled prompt without spending quota — use
  it to check what the model will actually receive.
- `--verbatim` sends codex nothing but the `$imagegen` trigger and your prompt,
  word for word: no wrapper, no reference note. Use it when the caller owns an
  exact prompt. It cannot be combined with `--edit`, `--aspect` or `--variants`.

## Writing the prompt

The script adds the scaffolding that forces a real image-tool call; the
`--prompt` text should be purely about the picture. Beyond that, codex's own
image skill responds well to a short labelled spec:

```
Subject: a lone red bicycle, paint worn to bare metal at the edges
Scene: leaning on a wet stone harbour wall, town lights beyond
Style: cinematic photograph, shallow depth of field
Lighting: dusk, low warm sun, reflections on wet stone
Composition: subject slightly left of centre, room above for a headline
Text (verbatim): "NO SPARE"
Avoid: people, watermarks, extra bicycles
```

Guidance that pays off:

- Quote any text that must appear in the image verbatim, and keep it short —
  long strings come back misspelled.
- List what must *not* appear; negative constraints work.
- For a transparent cutout, ask for a transparent background explicitly and save
  as `.png`.
- When editing, name the invariants: "change only the sky; keep the bicycle,
  framing and lighting identical."
- Iterate one change at a time, feeding the previous output back through
  `--edit`. Several changes in one prompt tend to redraw the whole image.

An `--edit` is a re-generation from the attached image, not a pixel-level patch:
framing, crop and fine detail drift even when the prompt says to keep them. Two
things reduce the drift — restating the original description alongside the
change, and keeping the chain short, since every pass compounds it. When a
detail must survive untouched, composite it back in afterwards rather than
trusting the model to preserve it.

## After generating

Look at the result before handing it over — `Read` the file, since the model
sometimes gets text, counts, or a specific requested detail wrong. If it misses,
re-run with a sharpened prompt or refine it with `--edit`.

## Failure modes

- **"the codex run produced no image"** — the model answered in text instead of
  calling its image tool. Re-run; make the prompt more explicitly a picture
  request, or raise `--reasoning`. Nothing was written, so nothing is stale.
- **"the codex account is out of quota"** — wait for the reset time codex prints,
  or use another account. Do not retry in a loop. Do not switch to the OpenAI
  API as a substitute.
- **"codex CLI not found on PATH"** — install codex and run `codex login`, or set
  `CODEX_BIN`.
- **Wrong dimensions** — `--aspect` alone is only a hint; add `--fit WxH`.
