#!/usr/bin/env bash # # build.sh: the images for "The Perfect Drug" # # A poetry-to-image project after the song by Nine Inch Nails (1997): longing, absence and a # green hunger, retold as a gothic adult comic in eight movements of three images each. The # lyrics are copyrighted and are not reproduced here; the scenes below are original # interpretations of the song's mood. Read the words at the source: # # Lyrics: https://genius.com/Nine-inch-nails-the-perfect-drug-lyrics # Model: https://replicate.com/black-forest-labs/flux-3-image # Mood: Gothic, absinthe, ruin, purple velvet and green liquor, wolves, night, full moon, # blood, subtle horror, dark underworld adult comics # # Two reference portraits (the Poet and the Green Lady) are generated first and then handed to # every shot that features them, so the cast stays consistent across the whole story. # # 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: perfect-drug--.jpg, ref-poet.jpg and ref-lady.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="The Perfect Drug" STYLE="Dark underground adult graphic novel art, gothic horror comic illustration: heavy black \ inks, rough crosshatching, scratchy linework over painterly gouache washes, brutal film-noir \ chiaroscuro, grime and paper grain. Palette of ink black, bruised purple velvet and sickly \ absinthe green, cold full moonlight, small accents of blood red. Crumbling gothic ruin, \ decadent and rotten, subtle creeping horror." NEGATIVE="One single full-bleed illustration: no text, no lettering, no speech balloons, no \ panels, no borders, no signature, no watermark." POET="The Poet: a gaunt pale man of about thirty with long lank raven hair, sharp hollow cheeks, \ a thin scar through his left eyebrow, wearing a frayed purple velvet frock coat with a high \ collar over an open black shirt." LADY="The Green Lady: a spectral adult woman born of absinthe vapour, luminous emerald eyes, very \ long hair drifting like green smoke, ashen skin, a high-necked gown of torn antique lace that \ dissolves into mist." # --------------------------------------------------------------------------------------- # Eight movements, three shots each. shot # --------------------------------------------------------------------------------------- declare -A CAST SCENE shot() { CAST["$1-$2"]=$3; SCENE["$1-$2"]=$4; } # I. The Hollow Hours shot 1 1 P "Wide view of a ruined gothic mansion hall at night, roof torn open to a huge full moon, \ the Poet sits alone and small in a rotting purple velvet armchair among stopped clocks and dead leaves." shot 1 2 P "Tight close-up of the Poet's sleepless face lit from one side by moonlight, red-rimmed \ eyes staring at nothing, cobwebs and shadow behind him." shot 1 3 - "Still life on a warped table: stopped pocket watch, guttered candle, an empty cut-crystal \ absinthe glass with a green dreg, a dead moth, moonlight through broken shutters." # II. The Green Pour shot 2 1 P "The Poet bends over a tarnished silver absinthe fountain in a derelict parlour hung with \ purple velvet, green light from the liquor underlighting his face." shot 2 2 - "Extreme close-up of the absinthe ritual: iced water dripping onto a sugar cube on a \ slotted silver spoon, green liquor clouding into swirling pale opal in the glass." shot 2 3 P "Seen from above, the Poet raises the glowing green glass to his lips, his shadow on the \ wall stretching into the shape of a wolf." # III. The Apparition shot 3 1 PL "The Green Lady unfurls out of the vapour rising from the glass and towers over the \ seated Poet in the dark parlour, her smoke hair filling the room." shot 3 2 L "Intimate close-up of the Green Lady's face emerging from green mist, emerald eyes wide \ open, a faint cruel smile, drops of liquor like tears." shot 3 3 PL "In profile, the Green Lady's pale fingers hover a hair's breadth from the Poet's cheek \ while he closes his eyes, moonlight between them." # IV. The Craving shot 4 1 PL "The Poet reaches out with both open hands toward the Green Lady, a clear gap between \ them and no touching, as she dissolves into green smoke and drifts away out of his reach among torn \ purple velvet curtains." shot 4 2 P "The Poet on his knees on a cracked chequered floor, clawing at the air, mouth open in a \ silent cry, green vapour slipping through his fingers." shot 4 3 - "A hand-shaped print of green frost on a black mirror, a shattered glass below it, spilled \ absinthe spreading like a living stain toward a thin thread of blood." # V. The Wolves shot 5 1 - "A pack of huge gaunt black wolves with green eyes prowls into a roofless ruined cathedral \ under an enormous full moon, breath steaming." shot 5 2 P "The Poet backs against a broken stone angel as wolves circle him in the moonlit ruin, \ low dramatic angle, coat torn." shot 5 3 - "Extreme close-up of a snarling wolf's muzzle, one glowing green eye, wet fangs, a bead \ of blood on its fur, moon reflected in the eye." # VI. The Fracture shot 6 1 P "The Poet faces a tall shattered gilt mirror; every shard reflects a different distorted \ version of him, one of them grinning." shot 6 2 PL "In the splintered mirror the Green Lady stands behind the Poet's reflection although \ the room behind him is empty." shot 6 3 - "Close-up of a pale bleeding palm clutching a long mirror shard, one emerald eye reflected \ in the shard, dark red drops on purple velvet." # VII. The Barren Night shot 7 1 P "The Poet staggers alone through a dead forest of black twisted trees and leaning graves, \ tiny beneath a colossal full moon, wolves far behind him." shot 7 2 P "Close view of the Poet sheltering against a mossy headstone in driving rain, wet hair over \ his face, looking up at the full moon with quiet longing, a green glass bottle in the grass beside him." shot 7 3 L "Far off between the black trees one single Green Lady, only one figure, seen from behind, \ small and faintly glowing as she walks away into the dark, thorny roots and ground fog in the \ foreground." # VIII. The Last Glass shot 8 1 PL "In the ruined hall at the end of night the Green Lady cradles the motionless Poet's head \ in her lap on a heap of purple velvet, sleeping wolves around them, the moon sinking." shot 8 2 L "The Green Lady rises alone over the ruin as dawn greys the sky, her form thinning into a \ vast plume of green smoke, sad and victorious." shot 8 3 - "An overturned absinthe glass on the abandoned purple velvet armchair, a last green drop \ about to fall, wolf paw prints in the dust, pale moon in a broken window." # --------------------------------------------------------------------------------------- # 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/perfect-drug-%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="" cast=${CAST[$key]} case $cast in P) who="The reference image shows the Poet: keep his face, hair and coat, but compose a completely new scene. $POET" ;; L) who="The reference image shows the Green Lady: keep her face, hair and gown, but compose a completely new scene. $LADY" ;; PL) who="The first reference image shows the Poet and the second the Green Lady: keep their faces, hair and clothes, but compose a completely new scene. $POET $LADY" ;; esac printf '"%s", a gothic comic. %s %s %s %s' "$TITLE" "${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_POET="$HERE/ref-poet.$FORMAT" REF_LADY="$HERE/ref-lady.$FORMAT" if ((REFS || DRY)) || [[ ! -f "$REF_POET" || ! -f "$REF_LADY" ]]; then say "" say "${C}${B}the cast${Z} ${D}(2 reference portraits)${Z}" : >"$TMP/refs.txt" for who in "$POET" "$LADY"; do printf 'Character reference portrait, half length, facing the viewer, plain dark 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_POET" mv -f "$(result "$TMP/refs" 2)" "$REF_LADY" say "${G}saved${Z} ref-poet.$FORMAT ref-lady.$FORMAT" fi fi # --------------------------------------------------------------------------------------- # 2. The story: one batch per cast, so each batch gets the right reference images # --------------------------------------------------------------------------------------- for cast in - P L PL; 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=() ;; P) label="the Poet" refs=("$REF_POET") ;; L) label="the Green Lady" refs=("$REF_LADY") ;; PL) label="the Poet and the Green Lady" refs=("$REF_POET" "$REF_LADY") ;; esac # a dry run before the portraits exist cannot attach them if ((DRY)) && [[ ! -f "$REF_POET" || ! -f "$REF_LADY" ]]; 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} perfect-drug-$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"