From e37b4163a9e6fd549ee78ce0e7e8dabe536f605a Mon Sep 17 00:00:00 2001 From: Anand Roy <86306690+andycandy@users.noreply.github.com> Date: Tue, 29 Jul 2025 17:23:58 +0530 Subject: [PATCH] imagen push --- quickstarts-js/Get_Started_Imagen.js | 242 +++++++++++++++++++++++++++ quickstarts-js/README.md | 2 +- 2 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 quickstarts-js/Get_Started_Imagen.js diff --git a/quickstarts-js/Get_Started_Imagen.js b/quickstarts-js/Get_Started_Imagen.js new file mode 100644 index 000000000..6c649c674 --- /dev/null +++ b/quickstarts-js/Get_Started_Imagen.js @@ -0,0 +1,242 @@ +/* Markdown (render) +##### 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. +*/ + +/* Markdown (render) +# Gemini API: Get started with image generation +*/ + +/* Markdown (render) +The `imagen-3.0-generate-002` model is Google's highest quality text-to-image model, featuring a number of new and improved capabilities. Imagen 3 can do the following: + +* Generate images with fine detail, rich lighting, and few distracting artifact +* Understand prompts written in natural language +* Generate images in a wide range of formats and styles +* Render text effectively + +This notebook is using the [Python SDK](https://googleapis.github.io/python-genai/#imagen). For the REST API, check out the [Get Started with Imagen](../Get_started_imagen_rest.ipynb) guide. +*/ + +/* Markdown (render) + +
| + ⚠️ + | + +
+ Image generation is a paid-only feature and won't work if you are on the free tier. Check the pricing page for more details.+ |
+
+
+
+
+
+*/
+
+/* Markdown (render)
+### Generate images with text
+Imagen 3 and 4 models are good at generating images with text. Here's an example with a comic strip:
+*/
+
+// [CODE STARTS]
+prompt = "A 3‑panel cosmic epic comic. Panel 1: Tiny 'Stardust' in nebula; radar shows anomaly (text 'ANOMALY DETECTED'), hull text 'stardust'. Pilot whispers. Panel 2: Bioluminescent leviathan emerges; console red text 'WARNING!'. Panel 3: Leviathan chases ship through asteroids; console red text 'SHIELD CRITICAL!', screen text 'EVADE!'. Pilot screams, SFX 'CRUNCH!', 'ROOOOAAARR!'.";
+
+numberOfImages = 1;
+aspectRatio = "1:1";
+personGeneration = "ALLOW_ADULT";
+
+result = await ai.models.generateImages({
+ model: MODEL_ID,
+ prompt: prompt,
+ config: {
+ numberOfImages: numberOfImages,
+ aspectRatio: aspectRatio,
+ personGeneration: personGeneration,
+ outputMimeType: "image/jpeg"
+ }
+});
+
+for (const generatedImage of result.generatedImages) {
+ const base64 = generatedImage.image.imageBytes;
+ console.image(base64);
+}
+
+// [CODE ENDS]
+
+/* Output Sample
+
+
+
+*/
+
+/* Markdown (render)
+Here's a second one. This time, the image saved in the notebook has been generated using Imagen 4 Ultra as there was quite a lot of text to process:
+*/
+
+// [CODE STARTS]
+prompt = `
+ a wall on which a colorful tag is drawn and that can be read as the first verse of Charles Baudelaire's poem "l'invitation au voyage":
+ Mon enfant, ma sœur,
+ Songe à la douceur
+ D’aller là -bas vivre ensemble !
+ Aimer à loisir,
+ Aimer et mourir
+ Au pays qui te ressemble !
+`;
+
+numberOfImages = 1;
+aspectRatio = "9:16";
+personGeneration = "ALLOW_ADULT";
+
+result = await ai.models.generateImages({
+ model: MODEL_ID,
+ prompt: prompt,
+ config: {
+ numberOfImages: numberOfImages,
+ aspectRatio: aspectRatio,
+ personGeneration: personGeneration,
+ outputMimeType: "image/jpeg"
+ }
+});
+
+for (const generatedImage of result.generatedImages) {
+ const base64 = generatedImage.image.imageBytes;
+ console.image(base64);
+}
+
+// [CODE ENDS]
+
+/* Output Sample
+
+*/
+
+/* Markdown (render)
+## Next Steps
+### Useful documentation references:
+
+To improve your prompting skills, check the [prompt guide](https://ai.google.dev/gemini-api/docs/imagen-prompt-guide) for great advices on creating your prompts.
+
+### Check those cool Imagen examples:
+Here are some Imagen examples to get your imagination started on how to use it in creative ways:
+* [Illustrate a book](https://github.com/google-gemini/cookbook/blob/main/examples/Book_illustration.ipynb): Use Gemini and Imagen to create illustration for an open-source book
+
+### Continue your discovery of the Gemini API
+
+Gemini is not only good at generating images, but also at understanding them. Check the [Spatial understanding](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Spatial_understanding.ipynb) guide for an introduction on those capabilities, and the [Video understanding](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Video_understanding.ipynb) one for video examples.
+*/
\ No newline at end of file
diff --git a/quickstarts-js/README.md b/quickstarts-js/README.md
index 1a2922067..80e03d7f0 100644
--- a/quickstarts-js/README.md
+++ b/quickstarts-js/README.md
@@ -17,5 +17,5 @@ Stay tuned, more JavaScript notebooks are on the way!
| Cookbook | Description | Features | Launch | Code File |
| --- | --- | --- | --- | --- |
| Get Started | A comprehensive introduction to the Gemini JS/TS SDK, demonstrating features such as text and multimodal prompting, token counting, system instructions, safety filters, multi-turn chat, output control, function calling, content streaming, file uploads, and using URL or YouTube video context. | Explore core Gemini capabilities in JS/TS | [](https://aistudio.google.com/apps/bundled/get_started?showPreview=true) |