Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions genai/image-generation/imggen-mmflash-edit-img-with-txt-img.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
const fs = require('fs');
const {GoogleGenAI, Modality} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
// const GOOGLE_CLOUD_LOCATION =
// process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';

const GOOGLE_CLOUD_LOCATION = 'global';
const FILE_NAME = 'test-data/example-image-eiffel-tower.png';

async function generateImage(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const image = fs.readFileSync(FILE_NAME);

const response = await client.models.generateContent({
model: 'gemini-2.5-flash-image-preview',
contents: [image, 'Edit this image to make it look like a cartoon'],
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});

console.log(response);

for (const part of response.candidates[0].content.parts) {
if (part.text) {
console.log(`${part.text}`);
} else if (part.inlineData) {
const outputDir = 'output-folder';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
const filename = `${outputDir}/bw-example-image.png`;
fs.writeFileSync(filename, imageBytes);
}
}

return response;
}
// Example response:
// Okay, I will edit this image to give it a cartoonish style, with bolder outlines, simplified details, and more vibrant colors.
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]

module.exports = {
generateImage,
};
69 changes: 69 additions & 0 deletions genai/image-generation/imggen-mmflash-locale-aware-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
const fs = require('fs');
const {GoogleGenAI, Modality} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION =
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';

async function generateImage(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await client.models.generateContent({
model: 'gemini-2.5-flash-image-preview',
contents: 'Generate a photo of a breakfast meal.',
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});

console.log(response);

for (const part of response.candidates[0].content.parts) {
if (part.text) {
console.log(`${part.text}`);
} else if (part.inlineData) {
const outputDir = 'output-folder';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
const filename = `${outputDir}/example-breakfast-meal.png`;
fs.writeFileSync(filename, imageBytes);
}
}

return response;
}
// Example response:
// Generates a photo of a vibrant and appetizing breakfast meal.
// The scene will feature a white plate with golden-brown pancakes
// stacked neatly, drizzled with rich maple syrup and ...
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]

module.exports = {
generateImage,
};
85 changes: 85 additions & 0 deletions genai/image-generation/imggen-mmflash-multiple-imgs-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]
const fs = require('fs');
const {GoogleGenAI, Modality} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
// const GOOGLE_CLOUD_LOCATION =
// process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';

const GOOGLE_CLOUD_LOCATION = 'global';

async function generateImage(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await client.models.generateContent({
model: 'gemini-2.5-flash-image-preview',
contents: 'Generate 3 images a cat sitting on a chair.',
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});

console.log(response);

const generatedFileNames = [];
let imageCounter = 1;

for (const part of response.candidates[0].content.parts) {
if (part.text) {
console.log(part.text);
} else if (part.inlineData) {
const outputDir = 'output-folder';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
const filename = `${outputDir}/example-cats-0${imageCounter}.png`;
fs.writeFileSync(filename, imageBytes);
generatedFileNames.push(filename);
console.log(`Saved image: ${filename}`);

imageCounter++;
}
}

return generatedFileNames;
}
// Example response:
// Image 1: A fluffy calico cat with striking green eyes is perched elegantly on a vintage wooden
// chair with a woven seat. Sunlight streams through a nearby window, casting soft shadows and
// highlighting the cat's fur.
//
// Image 2: A sleek black cat with intense yellow eyes is sitting upright on a modern, minimalist
// white chair. The background is a plain grey wall, putting the focus entirely on the feline's
// graceful posture.
//
// Image 3: A ginger tabby cat with playful amber eyes is comfortably curled up asleep on a plush,
// oversized armchair upholstered in a soft, floral fabric. A corner of a cozy living room with a
// [END googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]

module.exports = {
generateImage,
};
87 changes: 87 additions & 0 deletions genai/image-generation/imggen-mmflash-txt-and-img-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]
const fs = require('fs');
const {GoogleGenAI, Modality} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
// const GOOGLE_CLOUD_LOCATION =
// process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';

const GOOGLE_CLOUD_LOCATION = 'global';

async function savePaellaRecipe(response) {
const parts = response.candidates[0].content.parts;

let mdText = '';
const outputDir = 'output-folder';

for (let i = 0; i < parts.length; i++) {
const part = parts[i];

if (part.text) {
mdText += part.text + '\n';
} else if (part.inlineData) {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
const imagePath = `example-image-${i + 1}.png`;
const saveImagePath = `${outputDir}/${imagePath}`;

fs.writeFileSync(saveImagePath, imageBytes);
mdText += `![image](./${imagePath})\n`;
}
}
const mdFile = `${outputDir}/paella-recipe.md`;

fs.writeFileSync(mdFile, mdText);
console.log(`Saved recepie to: ${mdFile}`);
}

async function generateImage(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await client.models.generateContent({
model: 'gemini-2.5-flash-image-preview',
contents:
'Generate an illustrated recipe for a paella. Create images to go alongside the text as you generate the recipe',
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});
console.log(response);

await savePaellaRecipe(response);

return response;
}
// Example response:
// A markdown page for a Paella recipe(`paella-recipe.md`) has been generated.
// It includes detailed steps and several images illustrating the cooking process.
// [END googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]

module.exports = {
generateImage,
};
15 changes: 10 additions & 5 deletions genai/image-generation/imggen-mmflash-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION =
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';

async function generateContent(
async function generateImage(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.models.generateContentStream({
const response = await client.models.generateContentStream({
model: 'gemini-2.0-flash-exp',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The model used here is gemini-2.0-flash-exp, which is an experimental model. Other new samples in this PR use gemini-2.5-flash-image-preview. For consistency across samples, you might consider updating this model if the functionality is supported, or adding a comment explaining why this specific experimental model is used.

contents:
'Generate an image of the Eiffel tower with fireworks in the background.',
Expand All @@ -43,13 +43,18 @@ async function generateContent(

const generatedFileNames = [];
let imageIndex = 0;

for await (const chunk of response) {
const text = chunk.text;
const data = chunk.data;
if (text) {
console.debug(text);
} else if (data) {
const fileName = `generate_content_streaming_image_${imageIndex++}.png`;
const outputDir = 'output-folder';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const fileName = `${outputDir}/generate_content_streaming_image_${imageIndex++}.png`;
console.debug(`Writing response image to file: ${fileName}.`);
try {
fs.writeFileSync(fileName, data);
Expand All @@ -65,5 +70,5 @@ async function generateContent(
// [END googlegenaisdk_imggen_mmflash_with_txt]

module.exports = {
generateContent,
generateImage,
};
Loading