|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +// [START googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt] |
| 18 | +const fs = require('fs'); |
| 19 | +const {GoogleGenAI, Modality} = require('@google/genai'); |
| 20 | + |
| 21 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 22 | +const GOOGLE_CLOUD_LOCATION = |
| 23 | + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; |
| 24 | + |
| 25 | +async function generateImage( |
| 26 | + projectId = GOOGLE_CLOUD_PROJECT, |
| 27 | + location = GOOGLE_CLOUD_LOCATION |
| 28 | +) { |
| 29 | + const client = new GoogleGenAI({ |
| 30 | + vertexai: true, |
| 31 | + project: projectId, |
| 32 | + location: location, |
| 33 | + }); |
| 34 | + |
| 35 | + const response = await client.models.generateContent({ |
| 36 | + model: 'gemini-2.5-flash-image', |
| 37 | + contents: 'Generate 3 images of a cat sitting on a chair.', |
| 38 | + config: { |
| 39 | + responseModalities: [Modality.TEXT, Modality.IMAGE], |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + console.log(response); |
| 44 | + |
| 45 | + const generatedFileNames = []; |
| 46 | + let imageCounter = 1; |
| 47 | + |
| 48 | + for (const part of response.candidates[0].content.parts) { |
| 49 | + if (part.text) { |
| 50 | + console.log(part.text); |
| 51 | + } else if (part.inlineData) { |
| 52 | + const outputDir = 'output-folder'; |
| 53 | + if (!fs.existsSync(outputDir)) { |
| 54 | + fs.mkdirSync(outputDir, {recursive: true}); |
| 55 | + } |
| 56 | + const imageBytes = Buffer.from(part.inlineData.data, 'base64'); |
| 57 | + const filename = `${outputDir}/example-cats-0${imageCounter}.png`; |
| 58 | + fs.writeFileSync(filename, imageBytes); |
| 59 | + generatedFileNames.push(filename); |
| 60 | + console.log(`Saved image: ${filename}`); |
| 61 | + |
| 62 | + imageCounter++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return generatedFileNames; |
| 67 | +} |
| 68 | +// Example response: |
| 69 | +// Image 1: A fluffy calico cat with striking green eyes is perched elegantly on a vintage wooden |
| 70 | +// chair with a woven seat. Sunlight streams through a nearby window, casting soft shadows and |
| 71 | +// highlighting the cat's fur. |
| 72 | +// |
| 73 | +// Image 2: A sleek black cat with intense yellow eyes is sitting upright on a modern, minimalist |
| 74 | +// white chair. The background is a plain grey wall, putting the focus entirely on the feline's |
| 75 | +// graceful posture. |
| 76 | +// |
| 77 | +// Image 3: A ginger tabby cat with playful amber eyes is comfortably curled up asleep on a plush, |
| 78 | +// oversized armchair upholstered in a soft, floral fabric. A corner of a cozy living room with a |
| 79 | +// [END googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt] |
| 80 | + |
| 81 | +module.exports = { |
| 82 | + generateImage, |
| 83 | +}; |
0 commit comments