#!/usr/bin/env bash # # build.sh: the images for "White Rabbit" # # A poetry-to-image project after the song by Jefferson Airplane (written by Grace Slick, 1967), # which borrows its dream logic from Lewis Carroll's Wonderland. Here it becomes an overexcited, # psychedelic, funky cartoon in eight movements of three images each. The lyrics are copyrighted # and are not reproduced here; the scenes below are original interpretations. All characters are # original designs, and the heroine is a grown woman. Read the words at the source: # # Lyrics: https://genius.com/Jefferson-airplane-white-rabbit-lyrics # Model: https://replicate.com/black-forest-labs/flux-3-image # Look: classic slapstick animation crossed with flat cut-out adult sitcom cartoons, # 1960s psychedelic poster colours, high definition # # Two reference portraits (Alice and the White Rabbit) are generated first and then handed to # every shot that features them, so the cast stays consistent across the whole trip. # # Usage: ./build.sh [options] (REPLICATE_API_TOKEN must be set) # -p, --part N[,N...] only these movements (1-8) # -k, --shot N[,N...] only these shots (1-3) # -a, --aspect RATIO aspect ratio of the shots (default 3:2) # -r, --resolution RES 768sq, 1k, 1.5k, 2k or 4k (default 1.5k) # -j, --jobs N parallel predictions (default 4) # -f, --force regenerate shots that already exist # --refs regenerate the two reference portraits too (changes the cast) # -n, --dry-run print the prompts and payloads, generate nothing # -h, --help # # Output: white-rabbit--.jpg, ref-alice.jpg and ref-rabbit.jpg next to this file. # FLUX 3 has no seed input, so a rerun gives new takes on the same prompts; keep the images you # like, they are skipped. Web grounding is switched off so the model invents its own imagery. # When all 24 shots exist, the post's `draft` flag is cleared. set -Eeuo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROSE="${ROSE_PY:-$HERE/../../../rose.py}" MODEL="black-forest-labs/flux-3-image" ASPECT="3:2" RESOLUTION="1.5k" JOBS=4 FORMAT="jpg" QUALITY=92 FORCE=0 REFS=0 DRY=0 PART_ONLY="" SHOT_ONLY="" PARTS=8 SHOTS=3 # --------------------------------------------------------------------------------------- # The look and the cast, reused in every prompt # --------------------------------------------------------------------------------------- TITLE="White Rabbit" STYLE="Super colourful, wildly overexcited psychedelic funky cartoon in crisp high definition: \ the elastic squash-and-stretch slapstick of classic golden-age theatrical animation crossed with \ the flat construction-paper cut-out look of crude adult animated sitcoms. Thick wobbly black \ outlines, flat saturated shapes, huge googly eyes, smear frames, speed lines, impact stars and \ exploding confetti. 1960s psychedelic poster palette of hot magenta, acid lime, electric blue, \ tangerine and sunflower yellow, melting swirls and op-art rainbow spirals in the background." NEGATIVE="Entirely original characters, no existing cartoon or film characters. One single \ full-bleed illustration: no text, no lettering, no speech balloons, no panels, no borders, no \ signature, no watermark." ALICE="Alice: a tall grown woman in her late twenties with adult proportions, wild tangerine hair \ in two big bouncy buns, enormous excited eyes, a turquoise mini-length pinafore dress with yellow \ polka dots over a pink blouse, purple tights and chunky red platform boots." RABBIT="The White Rabbit: a lanky frantic white rabbit with extremely long floppy ears, bulging \ pink eyes, two big buck teeth, a lime-green checked waistcoat, an orange bow tie and a giant \ golden pocket watch on a chain." # --------------------------------------------------------------------------------------- # Eight movements, three shots each. shot # --------------------------------------------------------------------------------------- declare -A CAST SCENE shot() { CAST["$1-$2"]=$3; SCENE["$1-$2"]=$4; } # Recurring supporting characters, described the same way every time they appear QUEEN="a tiny furious Red Queen with a gigantic head, a bright crimson gown covered in hearts and \ a small golden crown" KNIGHT="a clumsy old White Knight with a droopy white moustache in dented shiny white-silver plate \ armour" # I. Down the Hole shot 1 1 AR "The White Rabbit dives headfirst into a swirling rainbow rabbit hole in a candy-coloured \ meadow while Alice sprints after him, legs spinning in a blur." shot 1 2 A "Alice tumbles in slow-motion freefall down an endless psychedelic tunnel full of floating \ teacups, armchairs, alarm clocks and playing cards, arms flailing, grinning." shot 1 3 - "A tiny golden key and a sparkling swirl-patterned potion bottle bounce on a three-legged \ glass table at the bottom of a spiral-striped hall full of doors of every size." # II. Bigger and Smaller shot 2 1 A "Giant Alice has grown so huge that her arms and boots burst out of the windows and roof of \ a tiny crooked cottage, bricks flying, eyes popping with surprise." shot 2 2 A "Tiny Alice, no bigger than a sugar cube, rides a spoon like a surfboard across a giant \ cup of rainbow tea splashing in great waves." shot 2 3 - "A table crowded with wobbling sparkly candies, swirl lollipops and fizzing potion bottles, \ each one stretching and shrinking the furniture around it in rubbery distortion. Only objects, no \ animals and no people." # III. The Rabbit's Rush shot 3 1 R "The White Rabbit gapes at his enormous pocket watch in comic panic, hopping a metre off \ the ground, eyes wide, ears shooting straight up with cheerful puffs of cartoon steam." shot 3 2 AR "A frantic chase through a forest of giant lollipop trees: the White Rabbit zooms ahead \ leaving a rainbow smoke trail while Alice skids around a corner behind him." shot 3 3 R "The White Rabbit runs on top of a gigantic melting clock face as the numbers fly off in \ all directions." # IV. The Caterpillar shot 4 1 A "Alice stares up at a plump purple-and-lime caterpillar lounging on a huge spotted \ mushroom, who blows enormous shimmering soap bubbles from a curly golden bubble pipe." shot 4 2 - "Extreme close-up of the smug caterpillar with half-closed eyes and a tiny pair of round \ tinted spectacles, rainbow soap bubbles floating all around his many wiggling feet." shot 4 3 A "Alice floats inside a gigantic soap bubble drifting over a landscape of glowing \ polka-dot mushrooms and swirling hills." # V. The Chessboard shot 5 1 A "Alice stands on a vast warped chessboard stretching into a swirling sky while huge living \ chess pieces with cartoon faces all point and shout orders at her." shot 5 2 - "Living chess pieces stampede across a bending checkered landscape, a bishop bouncing like \ a spring and a wooden chess knight horse whinnying with spiral eyes. Only chess pieces, no rabbits." shot 5 3 A "Alice cartwheels over the chessboard squares as they flip and ripple like a rainbow wave." # VI. The Red Queen shot 6 1 A "$QUEEN screams at Alice so hard that her tonsils wobble and the blast blows Alice's hair \ straight back. Only the Queen and Alice, no rabbits." shot 6 2 - "An army of googly-eyed playing cards with stick legs tumbles in a giant house-of-cards \ avalanche around an empty throne shaped like a heart. Only playing cards and the empty throne, no \ animals." shot 6 3 AR "The White Rabbit blows a long curly trumpet while Alice sticks her tongue out and runs \ past a croquet lawn where a pink flamingo mallet goes flying." # VII. The White Knight shot 7 1 A "$KNIGHT rides his horse backwards, talking nonsense, while Alice walks beside him looking \ baffled and amused." shot 7 2 - "$KNIGHT tumbles upside down off his bouncing horse in a burst of pots, pans, springs and \ stars. Only the knight and the horse, no rabbits." shot 7 3 A "Alice and $KNIGHT, upside down, hang from a tree whose branches are twisting rainbow ribbons, \ the sky spinning behind them. Only Alice and the knight, no rabbits." # VIII. The Dormouse Crescendo shot 8 1 - "A sleepy round dormouse suddenly leaps out of a giant teapot at a chaotic tea party, \ shouting with all its might, the table and crockery blasting into the air." shot 8 2 A "Alice sits on a hill looking up in joyful wonder as a giant fountain of flying books, \ planets, stars, flowers and rainbows swirls into the sky above her." shot 8 3 AR "Grand finale: Alice and the White Rabbit dance on top of a giant mushroom while all of \ Wonderland explodes into fireworks, spirals and confetti around them." # --------------------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------------------- if [[ -t 2 ]]; then B=$'\033[1m' D=$'\033[2m' R=$'\033[31m' G=$'\033[32m' Y=$'\033[33m' C=$'\033[36m' Z=$'\033[0m' else B="" D="" R="" G="" Y="" C="" Z="" fi say() { printf '%s\n' "$*" >&2; } die() { say "${R}error:${Z} $*"; exit 2; } usage() { sed -n '/^# Usage:/,/^# -h/p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; } squash() { tr -s '[:space:]' ' '; } file_of() { printf '%s/white-rabbit-%d-%d' "$HERE" "$1" "$2"; } # movement shot -> path, no extension have() { compgen -G "$(file_of "$1" "$2").*" >/dev/null; } shot_prompt() { # movement shot -> one-line prompt local key="$1-$2" cast who="" lead="A psychedelic cartoon." cast=${CAST[$key]} # naming the song in a prompt summons a rabbit, so only shots that want one get the title [[ $cast == *R* ]] && lead="\"$TITLE\", a psychedelic cartoon." case $cast in A) who="The reference image shows Alice: keep her face, hair and outfit, but compose a completely new scene. $ALICE" ;; R) who="The reference image shows the White Rabbit: keep his face and clothes, but compose a completely new scene. $RABBIT" ;; AR) who="The first reference image shows Alice and the second the White Rabbit: keep their faces and outfits, but compose a completely new scene. $ALICE $RABBIT" ;; esac printf '%s %s %s %s %s' "$lead" "${SCENE[$key]}" "$who" "$STYLE" "$NEGATIVE" | squash } run_rose() { # prompts_file out_dir aspect [reference images...] local prompts=$1 out=$2 aspect=$3 ref shift 3 local args=(replicate "$MODEL" --prompts-file "$prompts" --jobs "$JOBS" --timeout 900 --aspect-ratio "$aspect" --resolution "$RESOLUTION" --no-grounding --output-format "$FORMAT" --output-quality "$QUALITY" --output-dir "$out" --no-meta) for ref in "$@"; do args+=(--images "$ref"); done ((DRY)) && args+=(--dry-run) python3 "$ROSE" "${args[@]}" } result() { # out_dir index -> the file rose.py saved for that prompt, if any find "$1" -maxdepth 1 -type f -name "*-$(printf '%02d' "$2").*" 2>/dev/null | head -n1 || true } # --------------------------------------------------------------------------------------- # Options # --------------------------------------------------------------------------------------- while (($#)); do case "$1" in -p | --part) PART_ONLY="${2:?--part needs a value}"; shift 2 ;; -k | --shot) SHOT_ONLY="${2:?--shot needs a value}"; shift 2 ;; -a | --aspect) ASPECT="${2:?--aspect needs a value}"; shift 2 ;; -r | --resolution) RESOLUTION="${2:?--resolution needs a value}"; shift 2 ;; -j | --jobs) JOBS="${2:?--jobs needs a value}"; shift 2 ;; -f | --force) FORCE=1; shift ;; --refs) REFS=1; shift ;; -n | --dry-run) DRY=1; shift ;; -h | --help) usage; exit 0 ;; *) die "unknown option: $1 (try --help)" ;; esac done [[ -f "$ROSE" ]] || die "rose.py not found at $ROSE (set ROSE_PY)" command -v python3 >/dev/null || die "python3 is required" [[ -n "${REPLICATE_API_TOKEN:-}" ]] || die "REPLICATE_API_TOKEN is not set" if [[ -n "$PART_ONLY" ]]; then IFS=',' read -r -a WANT <<<"$PART_ONLY" for n in "${WANT[@]}"; do [[ "$n" =~ ^[1-8]$ ]] || die "movement must be 1-8, got '$n'"; done else WANT=(1 2 3 4 5 6 7 8) fi if [[ -n "$SHOT_ONLY" ]]; then IFS=',' read -r -a SHOT_WANT <<<"$SHOT_ONLY" for n in "${SHOT_WANT[@]}"; do [[ "$n" =~ ^[1-3]$ ]] || die "shot must be 1-3, got '$n'"; done else SHOT_WANT=(1 2 3) fi TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT failed=0 say "${B}${TITLE}${Z} ${D}${MODEL} · ${ASPECT} · ${RESOLUTION}${Z}" # --------------------------------------------------------------------------------------- # 1. The cast: two reference portraits # --------------------------------------------------------------------------------------- REF_ALICE="$HERE/ref-alice.$FORMAT" REF_RABBIT="$HERE/ref-rabbit.$FORMAT" if ((REFS || DRY)) || [[ ! -f "$REF_ALICE" || ! -f "$REF_RABBIT" ]]; then say "" say "${C}${B}the cast${Z} ${D}(2 reference portraits)${Z}" : >"$TMP/refs.txt" for who in "$ALICE" "$RABBIT"; do printf 'Cartoon character reference portrait, full body, facing the viewer, simple swirl backdrop. %s %s %s\n' \ "$who" "$STYLE" "$NEGATIVE" | squash >>"$TMP/refs.txt" printf '\n' >>"$TMP/refs.txt" done run_rose "$TMP/refs.txt" "$TMP/refs" "2:3" || die "could not generate the reference portraits" if ((!DRY)); then mv -f "$(result "$TMP/refs" 1)" "$REF_ALICE" mv -f "$(result "$TMP/refs" 2)" "$REF_RABBIT" say "${G}saved${Z} ref-alice.$FORMAT ref-rabbit.$FORMAT" fi fi # --------------------------------------------------------------------------------------- # 2. The story: one batch per cast, so each batch gets the right reference images # --------------------------------------------------------------------------------------- for cast in - A R AR; do todo=() for m in "${WANT[@]}"; do for k in "${SHOT_WANT[@]}"; do [[ "${CAST[$m-$k]}" == "$cast" ]] || continue if ((FORCE || DRY)) || ! have "$m" "$k"; then todo+=("$m $k"); fi done done ((${#todo[@]})) || continue case $cast in -) label="no cast" refs=() ;; A) label="Alice" refs=("$REF_ALICE") ;; R) label="the White Rabbit" refs=("$REF_RABBIT") ;; AR) label="Alice and the White Rabbit" refs=("$REF_ALICE" "$REF_RABBIT") ;; esac # a dry run before the portraits exist cannot attach them if ((DRY)) && [[ ! -f "$REF_ALICE" || ! -f "$REF_RABBIT" ]]; then refs=(); fi say "" say "${C}${B}${label}${Z} ${D}(${#todo[@]} shot(s))${Z}" prompts="$TMP/cast-$cast.txt" : >"$prompts" for item in "${todo[@]}"; do read -r m k <<<"$item" shot_prompt "$m" "$k" >>"$prompts" printf '\n' >>"$prompts" done if ! run_rose "$prompts" "$TMP/out-$cast" "$ASPECT" ${refs[@]+"${refs[@]}"}; then say "${Y}some shots failed, rerun to retry them${Z}" failed=1 fi ((DRY)) && continue i=0 for item in "${todo[@]}"; do i=$((i + 1)) read -r m k <<<"$item" src="$(result "$TMP/out-$cast" "$i")" [[ -n "$src" ]] || continue mv -f "$src" "$(file_of "$m" "$k").${src##*.}" say "${G}saved${Z} white-rabbit-$m-$k.${src##*.}" done done # --------------------------------------------------------------------------------------- # Publish the post once everything exists # --------------------------------------------------------------------------------------- if ((!DRY)); then missing=0 for ((m = 1; m <= PARTS; m++)); do for ((k = 1; k <= SHOTS; k++)); do have "$m" "$k" || missing=$((missing + 1)); done done if ((missing)); then say "" say "${Y}${missing} shot(s) still missing.${Z}" elif [[ -f "$HERE/index.md" ]] && grep -q '^draft: true$' "$HERE/index.md"; then sed -i 's/^draft: true$/draft: false/' "$HERE/index.md" say "" say "${G}All $((PARTS * SHOTS)) shots exist: draft flag cleared in index.md.${Z}" fi fi exit "$failed"