From fc0db475eb488d3f7713e18053569a6d0ae652f6 Mon Sep 17 00:00:00 2001 From: Seb-Lis Date: Fri, 21 Nov 2025 10:06:37 +0100 Subject: [PATCH] Uploading the code --- GeminiImageGen.py | 123 ++++++++++++++++ INSTALLATION.txt | 176 +++++++++++++++++++++++ README.md | 359 +++++++++++++++++++++++++++++++++++++++++++++- __init__.py | 3 + requirements.txt | 1 + 5 files changed, 660 insertions(+), 2 deletions(-) create mode 100644 GeminiImageGen.py create mode 100644 INSTALLATION.txt create mode 100644 __init__.py create mode 100644 requirements.txt diff --git a/GeminiImageGen.py b/GeminiImageGen.py new file mode 100644 index 0000000..e32d9a8 --- /dev/null +++ b/GeminiImageGen.py @@ -0,0 +1,123 @@ +import torch +import numpy as np +from PIL import Image +import io +import os +from google import genai +from google.genai import types + +class GeminiImageGen: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "api_key": ("STRING", {"multiline": False, "default": "INSERT_API_KEY_HERE"}), + "prompt": ("STRING", {"multiline": True, "default": "A futuristic city with flying cars"}), + "model": ( + [ + "gemini-3-pro-image-preview", + "gemini-2.5-flash-image", + ], + {"default": "gemini-3-pro-image-preview"}, + ), + "resolution": ( + ["1K", "2K", "4K"], + {"default": "1K"}, + ), + "aspect_ratio": ( + [ + "1:1", + "16:9", + "4:3", + "3:4", + "9:16", + "2:3", + "3:2", + "4:5", + "5:4", + "21:9", + ], + {"default": "1:1"}, + ), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + }, + } + + RETURN_TYPES = ("IMAGE",) + RETURN_NAMES = ("image",) + FUNCTION = "generate_image" + CATEGORY = "Gemini" + + def generate_image(self, api_key, prompt, model, resolution, aspect_ratio, seed): + # Configure the client + if api_key == "INSERT_API_KEY_HERE": + api_key = os.environ.get("GOOGLE_API_KEY") + + if not api_key: + raise ValueError("API Key is required. Please provide it in the node or set GOOGLE_API_KEY environment variable.") + + client = genai.Client(api_key=api_key) + + try: + # Configure proper image generation settings using ImageConfig + # This requires google-genai >= 1.51.0 + config = types.GenerateContentConfig( + response_modalities=['TEXT', 'IMAGE'], + image_config=types.ImageConfig( + aspect_ratio=aspect_ratio, + image_size=resolution # Must be uppercase: "1K", "2K", "4K" + ) + ) + + # Call the API + response = client.models.generate_content( + model=model, + contents=[prompt], + config=config + ) + + + generated_image = None + + # Extract image from response - the correct path is response.candidates[0].content.parts + if response.candidates and response.candidates[0].content and response.candidates[0].content.parts: + for part in response.candidates[0].content.parts: + if part.inline_data: + # Extract bytes directly from inline_data and create PIL Image + # as_image() returns incompatible Image type without .mode attribute + image_bytes = part.inline_data.data + generated_image = Image.open(io.BytesIO(image_bytes)) + break + + if generated_image is None: + raise RuntimeError("No image generated by Gemini API. The response did not contain inline image data.") + + # Convert PIL Image to Tensor + if generated_image.mode != "RGB": + generated_image = generated_image.convert("RGB") + + image = np.array(generated_image).astype(np.float32) / 255.0 + image = torch.from_numpy(image)[None,] + + return (image,) + + except Exception as e: + error_msg = str(e) + if "429" in error_msg or "RESOURCE_EXHAUSTED" in error_msg: + raise RuntimeError(f"Gemini API Quota Exceeded (429). You are likely on the Free Tier and have exhausted your limit or this model is not available on the Free Tier. Please check your billing at Google AI Studio. Details: {error_msg}") + elif "404" in error_msg or "NOT_FOUND" in error_msg: + raise RuntimeError(f"Gemini Model Not Found (404). The model '{model}' might not be available in your region or for your API key. Details: {error_msg}") + else: + raise RuntimeError(f"Error generating image with Gemini API: {error_msg}") + +# Node Mappings +NODE_CLASS_MAPPINGS = { + "GeminiImageGen": GeminiImageGen +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "GeminiImageGen": "Gemini 3 Pro Image Generator" +} diff --git a/INSTALLATION.txt b/INSTALLATION.txt new file mode 100644 index 0000000..af4bc8b --- /dev/null +++ b/INSTALLATION.txt @@ -0,0 +1,176 @@ +=================================================================== +GEMINI IMAGE GENERATION NODE - INSTALLATION GUIDE +=================================================================== + +This custom node allows you to generate images using Google's Gemini API +directly within ComfyUI, supporting resolutions up to 4K. + +------------------------------------------------------------------- +QUICK START - COPY & PASTE THESE COMMANDS +------------------------------------------------------------------- + +1. Navigate to your ComfyUI directory in terminal/command prompt + +2. For Windows (PowerShell or CMD): + + cd C:\Users\[YourUsername]\Documents\ComfyUI + .venv\Scripts\activate + pip install --upgrade google-genai + +3. For Linux/Mac: + + cd ~/ComfyUI + source venv/bin/activate + pip install --upgrade google-genai + +4. Verify installation (optional): + + pip show google-genai + +5. Set API key as environment variable (recommended): + + Windows (PowerShell): + $env:GOOGLE_API_KEY="YOUR_API_KEY_HERE" + + Windows (CMD): + set GOOGLE_API_KEY=YOUR_API_KEY_HERE + + Linux/Mac: + export GOOGLE_API_KEY="YOUR_API_KEY_HERE" + +6. Restart ComfyUI + +------------------------------------------------------------------- +REQUIREMENTS +------------------------------------------------------------------- + +- ComfyUI installed and working +- Python 3.8 or higher +- Google AI Studio API key (free tier available) +- Internet connection + +------------------------------------------------------------------- +INSTALLATION STEPS +------------------------------------------------------------------- + +1. INSTALL THE NODE + + Copy the "Google Gemini API" folder to your ComfyUI custom nodes directory: + + Windows: + C:\Users\[YourUsername]\Documents\ComfyUI\custom_nodes\ + + Linux/Mac: + ~/ComfyUI/custom_nodes/ + +2. INSTALL PYTHON DEPENDENCIES + + Open a terminal/command prompt and navigate to your ComfyUI directory, then run: + + Windows (PowerShell): + .\venv\Scripts\activate + pip install --upgrade google-genai + + Linux/Mac: + source venv/bin/activate + pip install --upgrade google-genai + + IMPORTANT: You need google-genai version 1.51.0 or higher for 4K support. + + To verify the version: + pip show google-genai + +3. GET YOUR GOOGLE API KEY + + a) Go to: https://aistudio.google.com/app/apikey + b) Sign in with your Google account + c) Click "Create API Key" + d) Copy the generated key (starts with "AIza...") + + BILLING NOTE: Image generation requires billing to be enabled. + - Free tier has ZERO quota for image generation + - You MUST enable billing at: https://console.cloud.google.com/billing + - Set up budget alerts to avoid unexpected charges + +4. CONFIGURE THE NODE + + Option A: In ComfyUI Node + - Paste your API key directly into the "api_key" field + + Option B: Environment Variable (Recommended for security) + - Set GOOGLE_API_KEY environment variable + - The node will automatically use it if the field says "INSERT_API_KEY_HERE" + + Windows (PowerShell): + $env:GOOGLE_API_KEY="YOUR_API_KEY_HERE" + + Linux/Mac: + export GOOGLE_API_KEY="YOUR_API_KEY_HERE" + +5. RESTART COMFYUI + + After installing dependencies, restart ComfyUI completely. + +------------------------------------------------------------------- +NODE FEATURES +------------------------------------------------------------------- + +- Models: + * gemini-3-pro-image-preview (recommended, supports 4K) + * gemini-2.5-flash-image + +- Resolutions: + * 1K: ~1024 pixels + * 2K: ~2048 pixels + * 4K: ~4096 pixels (e.g., 5504x3072 for 16:9) + +- Aspect Ratios: + * 1:1, 16:9, 4:3, 3:4, 9:16, 2:3, 3:2, 4:5, 5:4, 21:9 + +------------------------------------------------------------------- +BUDGET PROTECTION +------------------------------------------------------------------- + +To avoid unexpected charges, set up a budget alert: + +1. Go to: https://console.cloud.google.com/billing +2. Select your billing account +3. Click "Budgets & alerts" (left menu) +4. Click "Create Budget" +5. Set monthly limit (e.g., $5 or $10) +6. Enable email alerts at 50%, 90%, 100% + +Image generation costs vary by resolution. 4K images cost more than 1K. + +------------------------------------------------------------------- +TROUBLESHOOTING +------------------------------------------------------------------- + +ERROR: "RESOURCE_EXHAUSTED" or "429" error +SOLUTION: Enable billing for your Google Cloud project. Free tier has + zero quota for image generation. + +ERROR: "GenerateContentResponse object has no attribute 'parts'" +SOLUTION: Outdated SDK. Run: pip install --upgrade google-genai + +ERROR: "Image object has no attribute 'mode'" +SOLUTION: Update GeminiImageGen.py to latest version from this folder. + +ERROR: "ImageConfig not found" +SOLUTION: Upgrade google-genai to 1.51.0+: pip install --upgrade google-genai + +ERROR: Images always 1K regardless of setting +SOLUTION: Ensure google-genai is version 1.51.0 or higher. + +------------------------------------------------------------------- +SUPPORT +------------------------------------------------------------------- + +For issues with: +- This node: Check the GeminiImageGen.py file for updates +- Gemini API: https://ai.google.dev/gemini-api/docs/image-generation +- Billing: https://console.cloud.google.com/billing + +=================================================================== +Last Updated: 2025-11-20 +=================================================================== diff --git a/README.md b/README.md index 3f993ce..7066368 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,357 @@ -# ComfyUI_Gemini_3_Pro_API_node -Gemini_3_Pro_API_node for ComfyUI +# ComfyUI\_Gemini\_3\_Pro\_API\_node + +Gemini\_3\_Pro\_API\_node for ComfyUI +=================================================================== + +GEMINI IMAGE GENERATION NODE - INSTALLATION GUIDE + +=================================================================== + + + +This custom node allows you to generate images using Google's Gemini API + +directly within ComfyUI, supporting resolutions up to 4K. + + + +------------------------------------------------------------------- + +QUICK START - COPY \& PASTE THESE COMMANDS + +------------------------------------------------------------------- + + + +1\. Navigate to your ComfyUI directory in terminal/command prompt + + + +2\. For Windows (PowerShell or CMD): + +  + +  cd C:\\Users\\\[YourUsername]\\Documents\\ComfyUI + +  .venv\\Scripts\\activate + +  pip install --upgrade google-genai + +  + +3\. For Linux/Mac: + +  + +  cd ~/ComfyUI + +  source venv/bin/activate + +  pip install --upgrade google-genai + + + +4\. Verify installation (optional): + +  + +  pip show google-genai + + + +5\. Set API key as environment variable (recommended): + +  + +  Windows (PowerShell): + +  $env:GOOGLE\_API\_KEY="YOUR\_API\_KEY\_HERE" + +  + +  Windows (CMD): + +  set GOOGLE\_API\_KEY=YOUR\_API\_KEY\_HERE + +  + +  Linux/Mac: + +  export GOOGLE\_API\_KEY="YOUR\_API\_KEY\_HERE" + + + +6\. Restart ComfyUI + + + +------------------------------------------------------------------- + +REQUIREMENTS + +------------------------------------------------------------------- + + + +\- ComfyUI installed and working + +\- Python 3.8 or higher + +\- Google AI Studio API key (free tier available) + +\- Internet connection + + + +------------------------------------------------------------------- + +INSTALLATION STEPS + +------------------------------------------------------------------- + + + +1\. INSTALL THE NODE + +  + +  Copy the "Google Gemini API" folder to your ComfyUI custom nodes directory: + +  + +  Windows: + +  C:\\Users\\\[YourUsername]\\Documents\\ComfyUI\\custom\_nodes\\ + +  + +  Linux/Mac: + +  ~/ComfyUI/custom\_nodes/ + + + +2\. INSTALL PYTHON DEPENDENCIES + +  + +  Open a terminal/command prompt and navigate to your ComfyUI directory, then run: + +  + +  Windows (PowerShell): + +  .\\venv\\Scripts\\activate + +  pip install --upgrade google-genai + +  + +  Linux/Mac: + +  source venv/bin/activate + +  pip install --upgrade google-genai + +  + +  IMPORTANT: You need google-genai version 1.51.0 or higher for 4K support. + +  + +  To verify the version: + +  pip show google-genai + + + +3\. GET YOUR GOOGLE API KEY + +  + +  a) Go to: https://aistudio.google.com/app/apikey + +  b) Sign in with your Google account + +  c) Click "Create API Key" + +  d) Copy the generated key (starts with "AIza...") + +  + +  BILLING NOTE: Image generation requires billing to be enabled. + +  - Free tier has ZERO quota for image generation + +  - You MUST enable billing at: https://console.cloud.google.com/billing + +  - Set up budget alerts to avoid unexpected charges + + + +4\. CONFIGURE THE NODE + +  + +  Option A: In ComfyUI Node + +  - Paste your API key directly into the "api\_key" field + +  + +  Option B: Environment Variable (Recommended for security) + +  - Set GOOGLE\_API\_KEY environment variable + +  - The node will automatically use it if the field says "INSERT\_API\_KEY\_HERE" + +  + +  Windows (PowerShell): + +  $env:GOOGLE\_API\_KEY="YOUR\_API\_KEY\_HERE" + +  + +  Linux/Mac: + +  export GOOGLE\_API\_KEY="YOUR\_API\_KEY\_HERE" + + + +5\. RESTART COMFYUI + +  + +  After installing dependencies, restart ComfyUI completely. + + + +------------------------------------------------------------------- + +NODE FEATURES + +------------------------------------------------------------------- + + + +\- Models: + +  \* gemini-3-pro-image-preview (recommended, supports 4K) + +  \* gemini-2.5-flash-image + + + +\- Resolutions: + +  \* 1K: ~1024 pixels + +  \* 2K: ~2048 pixels + +  \* 4K: ~4096 pixels (e.g., 5504x3072 for 16:9) + + + +\- Aspect Ratios: + +  \* 1:1, 16:9, 4:3, 3:4, 9:16, 2:3, 3:2, 4:5, 5:4, 21:9 + + + +------------------------------------------------------------------- + +BUDGET PROTECTION + +------------------------------------------------------------------- + + + +To avoid unexpected charges, set up a budget alert: + + + +1\. Go to: https://console.cloud.google.com/billing + +2\. Select your billing account + +3\. Click "Budgets \& alerts" (left menu) + +4\. Click "Create Budget" + +5\. Set monthly limit (e.g., $5 or $10) + +6\. Enable email alerts at 50%, 90%, 100% + + + +Image generation costs vary by resolution. 4K images cost more than 1K. + + + +------------------------------------------------------------------- + +TROUBLESHOOTING + +------------------------------------------------------------------- + + + +ERROR: "RESOURCE\_EXHAUSTED" or "429" error + +SOLUTION: Enable billing for your Google Cloud project. Free tier has + +  zero quota for image generation. + + + +ERROR: "GenerateContentResponse object has no attribute 'parts'" + +SOLUTION: Outdated SDK. Run: pip install --upgrade google-genai + + + +ERROR: "Image object has no attribute 'mode'" + +SOLUTION: Update GeminiImageGen.py to latest version from this folder. + + + +ERROR: "ImageConfig not found" + +SOLUTION: Upgrade google-genai to 1.51.0+: pip install --upgrade google-genai + + + +ERROR: Images always 1K regardless of setting + +SOLUTION: Ensure google-genai is version 1.51.0 or higher. + + + +------------------------------------------------------------------- + +SUPPORT + +------------------------------------------------------------------- + + + +For issues with: + +\- This node: Check the GeminiImageGen.py file for updates + +\- Gemini API: https://ai.google.dev/gemini-api/docs/image-generation + +\- Billing: https://console.cloud.google.com/billing + + + +=================================================================== + +Last Updated: 2025-11-20 + +=================================================================== + + + diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e70da33 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .GeminiImageGen import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d234bd9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +google-genai