Skip to content

Instantly share code, notes, and snippets.

@patrickloeber
Last active September 14, 2025 04:10
Show Gist options
  • Select an option

  • Save patrickloeber/c4492974c6d625a6a57413810a605b12 to your computer and use it in GitHub Desktop.

Select an option

Save patrickloeber/c4492974c6d625a6a57413810a605b12 to your computer and use it in GitHub Desktop.

Revisions

  1. patrickloeber revised this gist Sep 2, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NanoBanana-Python.md
    Original file line number Diff line number Diff line change
    @@ -156,6 +156,6 @@ response1 = chat.send_message(
    # display / save image...

    # Continue chatting and editing
    response2 = chat.send_message("The cat should be in a sleeping position")
    response2 = chat.send_message("The cat should wear a funny party hat")
    # display / save image...
    ```
  2. patrickloeber renamed this gist Sep 2, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. patrickloeber created this gist Sep 2, 2025.
    161 changes: 161 additions & 0 deletions NanoBanana-JavaScript.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,161 @@
    ## Using Nano Banana with Python

    Guide on how to use Nano Banana aka Gemini 2.5 Flash Image in Python with the [Google GenAI SDK](https://github.com/googleapis/python-genai).

    A detailed blog post can be found on [TBD](#).

    More resources:

    - Get an API key from [Google AI Studio](https://aistudio.google.com/).
    - [Nano Banana Gemini API docs](https://ai.google.dev/gemini-api/docs/image-generation)

    ## Installation

    Install the SDK and Pillow (Pillow is used for image manipulation):

    ```bash
    pip install -U google-genai
    pip install Pillow
    ```

    ## Image Generation from Text

    ```python
    from google import genai
    from PIL import Image
    from io import BytesIO

    # Configure the client with your API key
    client = genai.Client(api_key="YOUR_API_KEY")

    prompt = """Create a photorealistic image of an orange cat
    with a green eyes, sitting on a couch."""

    # Call the API to generate content
    response = client.models.generate_content(
    model="gemini-2.5-flash-image-preview",
    contents=prompt,
    )

    # The response can contain both text and image data.
    # Iterate through the parts to find and save the image.
    for part in response.candidates[0].content.parts:
    if part.text is not None:
    print(part.text)
    elif part.inline_data is not None:
    image = Image.open(BytesIO(part.inline_data.data))
    image.save("cat.png")
    ```

    ## Image Editing with Text and Image Inputs

    ```python
    from google import genai
    from PIL import Image
    from io import BytesIO

    client = genai.Client(api_key="YOUR_API_KEY")

    prompt = """Using the image of the cat, create a photorealistic,
    street-level view of the cat walking along a sidewalk in a
    New York City neighborhood, with the blurred legs of pedestrians
    and yellow cabs passing by in the background."""

    image = Image.open("cat.png")

    # Pass both the text prompt and the image in the 'contents' list
    response = client.models.generate_content(
    model="gemini-2.5-flash-image-preview",
    contents=[prompt, image],
    )

    for part in response.candidates[0].content.parts:
    if part.text is not None:
    print(part.text)
    elif part.inline_data is not None:
    image = Image.open(BytesIO(part.inline_data.data))
    image.save("cat2.png")
    ```

    ## Photo restoration with Nano Banana

    ```python
    from google import genai
    from PIL import Image
    from io import BytesIO

    client = genai.Client(api_key="YOUR_API_KEY")

    prompt = "Restore and colorize this image from 1932"

    image = Image.open("lunch.jpg") # "Lunch atop a Skyscraper, 1932"

    response = client.models.generate_content(
    model="gemini-2.5-flash-image-preview",
    contents=[prompt, image],
    )

    for part in response.candidates[0].content.parts:
    if part.text is not None:
    print(part.text)
    elif part.inline_data is not None:
    image = Image.open(BytesIO(part.inline_data.data))
    image.save("lunch-restored.png")
    ```

    ## Working with Multiple Input Images

    ```python
    from google import genai
    from PIL import Image
    from io import BytesIO

    client = genai.Client(api_key="YOUR_API_KEY")

    prompt = "Make the girl wear this t-shirt. Leave the background unchanged."

    image1 = Image.open("girl.png")
    image2 = Image.open("tshirt.png")

    response = client.models.generate_content(
    model="gemini-2.5-flash-image-preview",
    contents=[prompt, image1, image2],
    )

    for part in response.candidates[0].content.parts:
    if part.text is not None:
    print(part.text)
    elif part.inline_data is not None:
    image = Image.open(BytesIO(part.inline_data.data))
    image.save("girl-with-shirt.png")

    ```

    ## Conversational Image Editing

    ```python
    from google import genai
    from PIL import Image
    from io import BytesIO

    client = genai.Client(api_key="YOUR_API_KEY")

    # Create a chat
    chat = client.chats.create(
    model="gemini-2.5-flash-image-preview"
    )

    # Make the first image edit
    response1 = chat.send_message(
    [
    "Change the cat to a bengal cat, leave everything else the same",
    Image.open("cat.png"),
    ]
    )

    # display / save image...

    # Continue chatting and editing
    response2 = chat.send_message("The cat should be in a sleeping position")
    # display / save image...
    ```