|
| 1 | +--- |
| 2 | +title: Google Gemini Image Generation |
| 3 | +description: Generate and edit images with Google Gemini 2.5 Flash Image using the AI SDK. |
| 4 | +tags: ['image-generation', 'google', 'gemini'] |
| 5 | +--- |
| 6 | + |
| 7 | +# Generate and Edit Images with Google Gemini 2.5 Flash |
| 8 | + |
| 9 | +This guide will show you how to generate and edit images with the AI SDK and Google's latest multimodal language model Gemini 2.5 Flash Image. |
| 10 | + |
| 11 | +## Generating Images |
| 12 | + |
| 13 | +As Gemini 2.5 Flash Image is a language model with multimodal capabilities, you can use the `generateText` or `streamText` functions (not `generateImage`) to create images. The model determines which modality to respond in based on your prompt and configuration. Here's how to create your first image: |
| 14 | + |
| 15 | +```ts |
| 16 | +import { google } from '@ai-sdk/google'; |
| 17 | +import { generateText } from 'ai'; |
| 18 | +import fs from 'node:fs'; |
| 19 | +import 'dotenv/config'; |
| 20 | + |
| 21 | +async function generateImage() { |
| 22 | + const result = await generateText({ |
| 23 | + model: google('gemini-2.5-flash-image-preview'), |
| 24 | + prompt: |
| 25 | + 'Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme', |
| 26 | + }); |
| 27 | + |
| 28 | + // Save generated images |
| 29 | + for (const file of result.files) { |
| 30 | + if (file.mediaType.startsWith('image/')) { |
| 31 | + const timestamp = Date.now(); |
| 32 | + const fileName = `generated-${timestamp}.png`; |
| 33 | + |
| 34 | + fs.mkdirSync('output', { recursive: true }); |
| 35 | + await fs.promises.writeFile(`output/${fileName}`, file.uint8Array); |
| 36 | + |
| 37 | + console.log(`Generated and saved image: output/${fileName}`); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +generateImage().catch(console.error); |
| 43 | +``` |
| 44 | + |
| 45 | +Here are some key points to remember: |
| 46 | + |
| 47 | +- Generated images are returned in the `result.files` array |
| 48 | +- Images are returned as `Uint8Array` data |
| 49 | +- The model leverages Gemini's world knowledge, so detailed prompts yield better results |
| 50 | + |
| 51 | +## Editing Images |
| 52 | + |
| 53 | +Gemini 2.5 Flash Image excels at editing existing images with natural language instructions. You can add elements, modify styles, or transform images while maintaining their core characteristics: |
| 54 | + |
| 55 | +```ts |
| 56 | +import { google } from '@ai-sdk/google'; |
| 57 | +import { generateText } from 'ai'; |
| 58 | +import fs from 'node:fs'; |
| 59 | +import 'dotenv/config'; |
| 60 | + |
| 61 | +async function editImage() { |
| 62 | + const editResult = await generateText({ |
| 63 | + model: google('gemini-2.5-flash-image-preview'), |
| 64 | + prompt: [ |
| 65 | + { |
| 66 | + role: 'user', |
| 67 | + content: [ |
| 68 | + { |
| 69 | + type: 'text', |
| 70 | + text: 'Add a small wizard hat to this cat. Keep everything else the same.', |
| 71 | + }, |
| 72 | + { |
| 73 | + type: 'image', |
| 74 | + // image: DataContent (string | Uint8Array | ArrayBuffer | Buffer) or URL |
| 75 | + image: new URL( |
| 76 | + 'https://raw.githubusercontent.com/vercel/ai/refs/heads/main/examples/ai-core/data/comic-cat.png', |
| 77 | + ), |
| 78 | + mediaType: 'image/jpeg', |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ], |
| 83 | + }); |
| 84 | + |
| 85 | + // Save the edited image |
| 86 | + const timestamp = Date.now(); |
| 87 | + fs.mkdirSync('output', { recursive: true }); |
| 88 | + |
| 89 | + for (const file of editResult.files) { |
| 90 | + if (file.mediaType.startsWith('image/')) { |
| 91 | + await fs.promises.writeFile( |
| 92 | + `output/edited-${timestamp}.png`, |
| 93 | + file.uint8Array, |
| 94 | + ); |
| 95 | + console.log(`Saved edited image: output/edited-${timestamp}.png`); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +editImage().catch(console.error); |
| 101 | +``` |
| 102 | + |
| 103 | +## What's Next? |
| 104 | + |
| 105 | +You've learned how to generate new images from text prompts and edit existing images using natural language instructions with Google's Gemini 2.5 Flash Image model. |
| 106 | + |
| 107 | +For more advanced techniques, integration patterns, and practical examples, check out our [Cookbook](/cookbook) where you'll find comprehensive guides for building sophisticated AI-powered applications. |
0 commit comments