Loading...
Loading...
Generate images with Gemini 3 Pro Image (Nano Banana Pro). Covers 4K generation, text rendering, grounded generation with Google Search, conversational editing, and cost optimization. Use when creating images, generating 4K images, editing images conversationally, fact-verified image generation, or image output tasks.
npx skill4agent add adaptationio/skrillz gemini-3-image-generationgemini-3-pro-image-previewgemini-3-multimodalgemini-3-pro-apigemini-3-pro-image-previewimport google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
# Use the image generation model
model = genai.GenerativeModel("gemini-3-pro-image-preview")
# Generate image
response = model.generate_content("A serene mountain landscape at sunset")
# Save image
if response.parts:
with open("generated_image.png", "wb") as f:
f.write(response.parts[0].inline_data.data)
print("Image saved!")import { GoogleGenerativeAI } from "@google/generative-ai";
import fs from "fs";
const genAI = new GoogleGenerativeAI("YOUR_API_KEY");
const model = genAI.getGenerativeModel({ model: "gemini-3-pro-image-preview" });
const result = await model.generateContent("A serene mountain landscape at sunset");
const imageData = result.response.parts[0].inlineData.data;
fs.writeFileSync("generated_image.png", Buffer.from(imageData, "base64"));
console.log("Image saved!");import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel(
"gemini-3-pro-image-preview",
generation_config={
"thinking_level": "high", # Best quality
"temperature": 1.0
}
)
# Generate image
prompt = """A futuristic cityscape at night with:
- Neon lights and holographic advertisements
- Flying vehicles
- Tall skyscrapers with unique architecture
- Rain-slicked streets reflecting the lights
- Cinematic, detailed, 4K quality"""
response = model.generate_content(prompt)
# Save image
if response.parts and hasattr(response.parts[0], 'inline_data'):
image_data = response.parts[0].inline_data.data
with open("futuristic_city.png", "wb") as f:
f.write(image_data)
print("Image generated successfully!")
else:
print("No image generated")references/generation-guide.md# Generate with 4K quality specification
prompt = """A photorealistic portrait of a scientist in a modern lab:
- 4K ultra-high definition
- Sharp focus on subject
- Soft bokeh background
- Professional studio lighting
- Fine detail in textures
- Cinema-grade quality"""
response = model.generate_content(prompt)
# 4K image will be generated
if response.parts:
with open("scientist_4k.png", "wb") as f:
f.write(response.parts[0].inline_data.data)references/resolution-guide.mdprompt = """Create a professional business card design with:
- Company name: "TechVision AI"
- Text: "Dr. Sarah Chen"
- Text: "Chief AI Officer"
- Text: "sarah.chen@techvision.ai"
- Text: "+1 (555) 123-4567"
- Modern, clean design
- Professional fonts
- Blue and white color scheme
- All text clearly readable"""
response = model.generate_content(prompt)
if response.parts:
with open("business_card.png", "wb") as f:
f.write(response.parts[0].inline_data.data)references/generation-guide.md# Enable Google Search grounding for factual accuracy
model_grounded = genai.GenerativeModel(
"gemini-3-pro-image-preview",
tools=[{"google_search_retrieval": {}}] # Enable grounding
)
prompt = """Generate an accurate image of the International Space Station
with Earth in the background. Use current ISS configuration."""
response = model_grounded.generate_content(prompt)
if response.parts:
with open("iss_grounded.png", "wb") as f:
f.write(response.parts[0].inline_data.data)
# Check if grounding was used
if hasattr(response, 'grounding_metadata'):
print(f"Grounding sources used: {len(response.grounding_metadata.grounding_chunks)}")references/grounded-generation.mdmodel = genai.GenerativeModel("gemini-3-pro-image-preview")
# Start a chat session for conversational editing
chat = model.start_chat()
# First generation
response1 = chat.send_message("Create a cozy coffee shop interior")
if response1.parts:
with open("coffee_shop_v1.png", "wb") as f:
f.write(response1.parts[0].inline_data.data)
# Refine the image
response2 = chat.send_message("Add more plants and warm lighting")
if response2.parts:
with open("coffee_shop_v2.png", "wb") as f:
f.write(response2.parts[0].inline_data.data)
# Further refinement
response3 = chat.send_message("Make it more minimalist, remove some decorations")
if response3.parts:
with open("coffee_shop_v3.png", "wb") as f:
f.write(response3.parts[0].inline_data.data)references/conversational-editing.md# 16:9 aspect ratio (4K supported)
prompt_169 = "A cinematic landscape in 16:9 aspect ratio, 4K quality"
# Square aspect ratio
prompt_square = "A square logo design for a tech company"
# Portrait orientation
prompt_portrait = "A portrait-oriented movie poster"
response = model.generate_content(prompt_169)
# Image will be generated in specified ratiodef generate_with_cost_tracking(prompt):
"""Generate image and track costs"""
response = model.generate_content(prompt)
# Calculate cost
usage = response.usage_metadata
input_cost = (usage.prompt_token_count / 1_000_000) * 2.00
output_cost = (usage.candidates_token_count / 1_000_000) * 9.00
image_cost = 0.134 # Per image
total_cost = input_cost + output_cost + image_cost
print(f"Input tokens: {usage.prompt_token_count} (${input_cost:.6f})")
print(f"Output tokens: {usage.candidates_token_count} (${output_cost:.6f})")
print(f"Image cost: ${image_cost:.6f}")
print(f"Total: ${total_cost:.6f}")
return response
response = generate_with_cost_tracking("A beautiful sunset over mountains")references/pricing-optimization.mdimport google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel("gemini-3-pro-image-preview")
prompts = [
"A serene mountain lake at dawn",
"A bustling market in Morocco",
"A futuristic robot assistant",
"An abstract geometric pattern"
]
for i, prompt in enumerate(prompts):
print(f"Generating image {i+1}/{len(prompts)}: {prompt}")
response = model.generate_content(prompt)
if response.parts:
with open(f"generated_{i+1}.png", "wb") as f:
f.write(response.parts[0].inline_data.data)
print(f" Saved: generated_{i+1}.png")from google.api_core import exceptions
def safe_image_generation(prompt):
"""Generate image with error handling"""
try:
response = model.generate_content(prompt)
if not response.parts:
return {"success": False, "error": "No image generated"}
if not hasattr(response.parts[0], 'inline_data'):
return {"success": False, "error": "Invalid response format"}
return {
"success": True,
"image_data": response.parts[0].inline_data.data,
"mime_type": response.parts[0].inline_data.mime_type
}
except exceptions.InvalidArgument as e:
return {"success": False, "error": f"Invalid prompt: {e}"}
except exceptions.ResourceExhausted as e:
return {"success": False, "error": f"Rate limit exceeded: {e}"}
except Exception as e:
return {"success": False, "error": f"Error: {e}"}response.partsinline_data