Lumen & Gesso / website · lumengesso.com / scripts / generate-pet-photo.mjs
| 1 | #!/usr/bin/env node |
| 2 | // Generate raw "phone-snap" pet reference photos via Nano Banana. |
| 3 | // Output mimics what a customer would upload: natural lighting, real |
| 4 | // settings, no studio gloss. These become the REFERENCE input for |
| 5 | // generate-style-examples.mjs. |
| 6 | // |
| 7 | // Usage: |
| 8 | // GEMINI_API_KEY=AIza... node scripts/generate-pet-photo.mjs |
| 9 | // |
| 10 | // Optional: |
| 11 | // ONLY=owl,goat restrict to specific jobs by id |
| 12 | |
| 13 | import fs from 'node:fs'; |
| 14 | import path from 'node:path'; |
| 15 | import { fileURLToPath } from 'node:url'; |
| 16 | |
| 17 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 18 | const ROOT = path.resolve(__dirname, '..'); |
| 19 | const GEMINI_KEY = process.env.GEMINI_API_KEY; |
| 20 | if (!GEMINI_KEY) { |
| 21 | console.error('GEMINI_API_KEY is not set.'); |
| 22 | process.exit(1); |
| 23 | } |
| 24 | |
| 25 | const MODEL = 'gemini-3-pro-image'; |
| 26 | const PORTRAITS_DIR = path.join(ROOT, 'public/portraits'); |
| 27 | const ONLY = process.env.ONLY |
| 28 | ? new Set(process.env.ONLY.split(',').map((s) => s.trim())) |
| 29 | : null; |
| 30 | |
| 31 | // Each job describes one pet — fed straight into a photo-realism prompt. |
| 32 | // `out` is what the file is named under /public/portraits/. The subject |
| 33 | // description should read like a real-life caption, with concrete |
| 34 | // details (breed, coat, pose, setting, light) so the model commits to a |
| 35 | // believable still rather than a generic stock-art rendering. |
| 36 | const JOBS = [ |
| 37 | { |
| 38 | id: 'owl', |
| 39 | out: 'before-owl.jpg', |
| 40 | subject: |
| 41 | 'a tawny barn owl perched on a rough-hewn wooden beam, head turned slightly toward camera, alert amber eyes, soft feather detail, dim golden sunset light streaming through an open hayloft window behind it', |
| 42 | }, |
| 43 | { |
| 44 | id: 'goat', |
| 45 | out: 'before-goat.jpg', |
| 46 | subject: |
| 47 | 'a Nigerian Dwarf goat with chestnut-and-white markings standing on a grassy pasture, head tilted curiously toward the camera, soft floppy ears, late-afternoon golden-hour light, distant fence and rolling hills out of focus behind', |
| 48 | }, |
| 49 | { |
| 50 | id: 'ferret', |
| 51 | out: 'before-ferret.jpg', |
| 52 | subject: |
| 53 | 'a sable-mask ferret lying relaxed on a cream linen blanket, looking up directly at the camera, small dark eyes bright, soft window light catching the silvery fur, a small wooden bookshelf softly out of focus behind', |
| 54 | }, |
| 55 | { |
| 56 | id: 'fox', |
| 57 | out: 'before-fox.jpg', |
| 58 | subject: |
| 59 | 'a young red fox sitting alert on a moss-covered forest floor, looking forward toward the camera, ears perked, fluffy white-tipped tail tucked around feet, soft dappled morning light filtering through trees', |
| 60 | }, |
| 61 | // ─── Testimonial dogs (homepage review carousel) ────────────────── |
| 62 | { |
| 63 | id: 'tucker', |
| 64 | out: 'before-tucker.jpg', |
| 65 | subject: |
| 66 | 'a fluffy golden retriever puppy sitting on a sunlit wooden porch step, head tilted slightly toward the camera, soft amber eyes and a little wet nose, warm late-afternoon light, a few potted plants softly out of focus behind', |
| 67 | }, |
| 68 | { |
| 69 | id: 'willow', |
| 70 | out: 'before-willow.jpg', |
| 71 | subject: |
| 72 | 'a black-and-white border collie lying on a worn living-room rug, chin resting on her paws, looking up directly at the camera with bright attentive eyes, soft window light, a couch edge and a leafy houseplant out of focus behind', |
| 73 | }, |
| 74 | { |
| 75 | id: 'gus', |
| 76 | out: 'before-gus.jpg', |
| 77 | subject: |
| 78 | 'a Pembroke Welsh corgi sitting on a kitchen floor, big ears perked up, tongue out in a happy open-mouth grin, looking toward the camera, soft daylight from a nearby window, pale cabinets softly out of focus behind', |
| 79 | }, |
| 80 | { |
| 81 | id: 'nova', |
| 82 | out: 'before-nova.jpg', |
| 83 | subject: |
| 84 | 'a Bernese mountain dog resting on a back-yard deck, head turned toward the camera, calm dark eyes, glossy tricolor coat, soft overcast daylight, a wooden railing and green shrubs out of focus behind', |
| 85 | }, |
| 86 | { |
| 87 | id: 'waffle', |
| 88 | out: 'before-waffle.jpg', |
| 89 | subject: |
| 90 | 'a smooth-coat dachshund standing on a tufted armchair, looking alertly toward the camera, long body and soft floppy ears, warm lamp light from the side, a cozy living-room wall softly out of focus behind', |
| 91 | }, |
| 92 | ]; |
| 93 | |
| 94 | async function generate(job) { |
| 95 | const outPath = path.join(PORTRAITS_DIR, job.out); |
| 96 | |
| 97 | const prompt = |
| 98 | `A natural, candid pet photograph — the kind a customer would casually snap with their phone. ` + |
| 99 | `Composition: head-and-shoulders to bust crop, the subject occupying roughly the centre 60-75% of a square frame with comfortable margin on all four sides. ` + |
| 100 | `Subject: ${job.subject}. ` + |
| 101 | `Style: realistic photographic quality, soft natural light, narrow depth of field, slight grain, no text, no watermark, no studio lighting, no people, no other animals. ` + |
| 102 | `Aspect ratio: square (1:1).`; |
| 103 | |
| 104 | const body = { |
| 105 | contents: [{ parts: [{ text: prompt }] }], |
| 106 | generationConfig: { responseModalities: ['image', 'text'] }, |
| 107 | }; |
| 108 | |
| 109 | const url = `https://generativelanguage.googleapis.com/v1beta/models/${MODEL}:generateContent`; |
| 110 | const r = await fetch(url, { |
| 111 | method: 'POST', |
| 112 | headers: { |
| 113 | 'x-goog-api-key': GEMINI_KEY, |
| 114 | 'content-type': 'application/json', |
| 115 | }, |
| 116 | body: JSON.stringify(body), |
| 117 | }); |
| 118 | if (!r.ok) { |
| 119 | const text = await r.text(); |
| 120 | throw new Error(`${r.status} ${r.statusText}: ${text.slice(0, 400)}`); |
| 121 | } |
| 122 | |
| 123 | const json = await r.json(); |
| 124 | const parts = json?.candidates?.[0]?.content?.parts ?? []; |
| 125 | let b64 = null; |
| 126 | for (const part of parts) { |
| 127 | const data = part?.inline_data?.data ?? part?.inlineData?.data; |
| 128 | if (data) { |
| 129 | b64 = data; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | if (!b64) throw new Error('No image data in Gemini response'); |
| 134 | |
| 135 | fs.writeFileSync(outPath, Buffer.from(b64, 'base64')); |
| 136 | console.log(` ✓ ${path.relative(ROOT, outPath)}`); |
| 137 | } |
| 138 | |
| 139 | const targets = ONLY ? JOBS.filter((j) => ONLY.has(j.id)) : JOBS; |
| 140 | console.log(`Model: ${MODEL}`); |
| 141 | console.log(`Jobs: ${targets.map((t) => t.id).join(', ')}`); |
| 142 | |
| 143 | for (const job of targets) { |
| 144 | console.log(`\n→ ${job.id}`); |
| 145 | try { |
| 146 | await generate(job); |
| 147 | } catch (err) { |
| 148 | console.error(` ✗ ${job.id}: ${err.message}`); |
| 149 | } |
| 150 | } |
| 151 | console.log('\nDone.'); |