IKORG

← Practice · September 1, 2026

How to Generate Images from the Terminal with a ChatGPT Subscription

Codex can generate images, but hides them in a sandbox. Here is a script that runs generation in the background and retrieves the finished PNG into the project.

5 min readtools

A follow-up to the note about GPT in Claude Code: login is already configured, the subscription is already paid for—and it turns out you can use the same subscription to generate images. You do not need a separate image generation service.

This is useful when you do not need an art-directed illustration, just a functional image: an article cover, icon, background, or placeholder in a mockup. I use it to generate covers for articles and logos for landing page drafts.

What’s the catch?

Codex CLI can call the image generator. But it puts the result inside its own sandbox—in ~/.codex/generated_images/<session id>/, not where you are working. Asking it to “save the file in the project” is pointless: with a regular user account, it has no permissions for network namespaces, and its own shell fails with bwrap: loopback: Failed RTM_NEWADDR.

The good news is that the image is already on disk. You just need to retrieve it yourself.

The script

Put this in ~/.local/bin/gpt-image:

#!/usr/bin/env bash
set -euo pipefail
export PATH="$HOME/.local/node/bin:$PATH"

OUT="картинка-$(date +%H%M%S).png"; IN=""; BG=0; WAIT=""
while [ $# -gt 0 ]; do
  case "$1" in
    -o) OUT="$2"; shift 2;;
    -i) IN="$2"; shift 2;;        # source image to edit
    --bg) BG=1; shift;;           # run in the background
    --wait) WAIT="$2"; shift 2;;  # retrieve the result of background generation
    *) break;;
  esac
done

забрать() {  # $1 — run log
  local id png
  id="$(grep -o 'session id: [0-9a-f-]\{36\}' "$1" | head -1 | awk '{print $3}')"
  [ -z "$id" ] && { echo "no session id in log: $1" >&2; return 1; }
  png="$(ls -t "$HOME/.codex/generated_images/$id/"*.png 2>/dev/null | head -1)"
  [ -z "$png" ] && { echo "image not found" >&2; return 1; }
  cp "$png" "$OUT"; echo "$OUT"
}

if [ -n "$WAIT" ]; then забрать "$WAIT"; exit $?; fi

LOG="$(mktemp -d)/log.txt"
ЗАДАНИЕ="Generate an image based on the description. Do not run commands or try to save files.
Description: $*"

пуск() {
  if [ -n "$IN" ]; then
    printf '%s' "$ЗАДАНИЕ" | codex exec -s read-only --skip-git-repo-check -i "$IN" - >"$LOG" 2>&1
  else
    printf '%s' "$ЗАДАНИЕ" | codex exec -s read-only --skip-git-repo-check - >"$LOG" 2>&1
  fi
}

if [ "$BG" = 1 ]; then
  ( пуск ) >/dev/null 2>&1 &
  echo "GPT_IMAGE_LOG: $LOG"
  echo "ready in 1–3 minutes; retrieve with: gpt-image --wait $LOG -o $OUT"
else
  пуск; забрать "$LOG"
fi
chmod +x ~/.local/bin/gpt-image

How to use it

# generate an image
gpt-image -o лого.png "construction company logo, yellow hard hat, flat style, no text"

# edit an existing image
gpt-image -i было.png -o стало.png "put it on a solid dark blue background"

# in the background (more reliable in a Claude conversation—you will not hit the timeout)
gpt-image --bg -o обложка.png "dark blue background, seven glowing cards arranged in a circle"
gpt-image --wait /tmp/tmp.XXXX/log.txt -o обложка.png

Generation takes from forty seconds to three minutes. That is why --bg mode is not a luxury: the command execution tool in Claude Code usually times out before the image is ready.

What you should know in advance

The prompt is passed through stdin, not as an argument. The -i flag in codex exec takes multiple values and “swallows” the positional text: you will get No prompt provided via stdin and lose a minute.

The phrase “do not run commands” in the prompt saves a minute. Otherwise, the agent will dutifully try to save the file, hit the sandbox restriction, and spend time explaining why it could not do it.

It cannot download images from the internet—it can only generate them. The sandbox has no network access.

Adherence to the description is average. Ask for “a flat vector on blue,” and you may get a three-dimensional object on a transparent background. Arguing in the first prompt costs more than fixing it with a second one: -i результат.png "remove the volume, use a flat fill".

It is better not to request text in the image. The generator gets letters wrong—especially Cyrillic. If you need labels, it is more reliable to generate the background and overlay the text in SVG or CSS.

The storage directory keeps growing. ~/.codex/generated_images accumulates every image from every run. Clean it once a week—on a small disk, it is noticeable:

find ~/.codex/generated_images -maxdepth 1 -type d -mtime +7 -exec rm -rf {} +

When this is enough, and when it isn’t

Enough for: backgrounds, abstract images, icons, article covers, draft logos, and placeholders in mockups. In other words, anything where you need “an image that conveys the idea,” not a carefully controlled brand style.

Not enough for: precise diagrams, interfaces, anything containing text, or a recurring character in a consistent style. Diagrams are better drawn with code—SVG generated by a script is predictable and takes seconds to edit.