-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgenerate-reference-image.mjs
More file actions
93 lines (79 loc) · 3.48 KB
/
generate-reference-image.mjs
File metadata and controls
93 lines (79 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import fs from 'fs';
import { config } from 'dotenv';
config();
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY;
if (!GOOGLE_API_KEY) { console.error('Missing GOOGLE_API_KEY in .env'); process.exit(1); }
const IMAGE_PATH = process.argv[2] || '/Users/dylan/Desktop/projects/vibetotext/cosmic-entity-reference.png';
async function generateReferenceImage() {
// Read the planet screenshot
const imageBytes = fs.readFileSync(IMAGE_PATH);
const base64Image = imageBytes.toString('base64');
console.log('Calling Gemini 2.0 Flash image editing API...');
// Try multiple model IDs - Gemini image gen models
const models = [
'gemini-3-pro-image-preview',
];
let response;
for (const model of models) {
console.log(`Trying model: ${model}...`);
response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${GOOGLE_API_KEY}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{
parts: [
{
inlineData: {
mimeType: 'image/png',
data: base64Image,
}
},
{
text: `Remove the planet (the green sphere with buildings/village on it) from this image. Keep the cosmic entity/creature exactly as it is — its body, arms, robes, head, everything stays the same. Just erase the planet and fill in what would be behind it (the creature's body/robes and space background). The creature should look like it's holding nothing — empty hands cradling empty space.`
}
]
}],
generationConfig: {
responseModalities: ['IMAGE', 'TEXT'],
},
}),
}
);
if (response.ok) break;
const err = await response.text();
console.error(` ${model} failed (${response.status}):`, err.slice(0, 200));
}
if (!response.ok) {
console.error('All models failed');
return;
}
return handleResponse(response);
}
async function handleResponse(response) {
const data = await response.json();
// Find image parts in the response
for (const candidate of (data.candidates || [])) {
for (const part of (candidate.content?.parts || [])) {
if (part.inlineData) {
const outPath = '/Users/dylan/Desktop/projects/vibetotext/cosmic-entity-reference.png';
const imgBuffer = Buffer.from(part.inlineData.data, 'base64');
fs.writeFileSync(outPath, imgBuffer);
console.log(`Saved reference image to: ${outPath} (${(imgBuffer.length / 1024).toFixed(1)} KB)`);
// Also open it
const { exec } = await import('child_process');
exec(`open "${outPath}"`);
return outPath;
}
if (part.text) {
console.log('Model response:', part.text);
}
}
}
console.log('No image in response. Full response:');
console.log(JSON.stringify(data, null, 2).slice(0, 2000));
}
generateReferenceImage().catch(err => {
console.error('Error:', err.message);
});