Transparent PNG Stickers with Nano Banana Pro and Gemini interactions API
Generating images is easy. Getting clean transparent backgrounds for actual use—stickers, overlays, print-on-demand—is harder than it should be.
This guide shows how to generate production-ready transparent stickers using the Gemini Interactions API. The trick: generate on chromakey green, strip it with HSV detection.
Workflow:
- Generate an image with a chromakey green (#00FF00) background using Gemini Pro 3 Image Preview (Nano Banana Pro)
- Use HSV color space detection to accurately remove all green shades
- Apply morphological cleanup to remove edge artifacts
- Save as a proper transparent PNG
Prerequisites:
- Install dependencies:
pip install google-genai pillow scipy - Set your
GEMINI_API_KEYenvironment variable
Notebook available on GitHub
Why Chromakey Instead of ML Background Removal?#
- ML Background Removal: Uses extra model call to remove the background. Slower and more expensive. Edge Quality can be hit or miss.
- Chromakey + HSV: Uses a chromakey green background. Faster, cheaper, and more predictable. Excellent Edge Quality with white outline.
When you control generation, prompting for a specific background color beats running another model. Faster, cheaper, and more predictable.
Setup#
First, let's install the required dependencies and set up the Gemini client.
Python
# Install dependencies (uncomment if needed)
# !pip install google-genai pillow scipy
# Initialize the Gemini client
=
# Model for image generation
=
Helper Functions#
We'll create helper functions using HSV color space for more robust green screen detection that catches all shades of green.
Python
"""Decode base64 image data to PIL Image."""
=
return
"""Convert RGB array to HSV array efficiently."""
# Normalize RGB to 0-1 range
= / 255.0
, , = , ,
=
=
= -
# Hue calculation
=
# When max == r
= &
= % 360
# When max == g
= &
=
# When max == b
= &
=
# Saturation calculation
=
= /
# Value is just max
=
return
"""
Remove green screen using HSV color space for better detection.
HSV is much better for detecting color ranges because it separates
hue (color) from saturation (intensity) and value (brightness).
"""
# Convert to RGBA if not already
=
# Convert to numpy array
=
=
# Convert to HSV
=
, , = , ,
# Calculate hue distance (accounting for circular nature of hue)
=
=
# Create mask for green pixels
# Green if: hue is in range AND saturation is high enough AND value is high enough
=
# Apply morphological cleanup to remove edge artifacts
# Dilate the mask to catch anti-aliased edge pixels
=
# Optionally erode back (removes isolated noise)
=
# Make green pixels transparent
=
= 0
=
return
"""
Aggressive green removal that detects any pixel where green dominates.
This catches even darker or lighter greens, shadows with green tint, etc.
"""
=
=
, , = , ,
# A pixel is "green" if green channel significantly exceeds red and blue
# This catches all shades of green including shadows
= + 1 # +1 to avoid division by zero
= /
# Also check that green is the dominant channel
= &
# Combined mask
= &
# Expand mask to catch edge pixels
=
# Apply transparency
=
= 0
=
return
"""
Clean up semi-transparent edge pixels by making them fully transparent or opaque.
This removes the "halo" effect from anti-aliased edges.
"""
return
=
=
# Make semi-transparent pixels either fully transparent or fully opaque
= 0
= 255
=
return
"""Save image as PNG with transparency preserved."""
=
Generate a Sticker with Chromakey Green Screen#
The key is to instruct Gemini to generate the image with a chromakey green background. We use specific prompts to ensure clean edges and no green spill.
Python
"""
Load an image from a file path and return it as a content block for the API.
"""
# Determine mime type from file extension
, =
# Default to JPEG if unknown
=
# Read and base64 encode the image
=
return
"""
Generate a sticker-style image with chromakey green background.
"""
# Optimized prompt for chromakey extraction
= f
# Build the input content
# When input_images are provided, create a list with image content blocks followed by text
=
# Add the text prompt as the final content block
# No input images, just use the text prompt directly
=
# Call Gemini Interactions API
=
# Extract the generated image
return
Create a Sticker End-to-End#
Let's put it all together: generate, remove green screen with HSV detection, apply aggressive cleanup, and save.
Python
"""
Complete workflow to create a transparent sticker.
Uses a multi-pass approach:
1. Generate with optimized chromakey prompt
2. HSV-based green removal (catches color range)
3. Aggressive green removal (catches remaining green tints)
4. Edge cleanup to remove halos
"""
# Step 1: Generate image with green screen
=
# Optionally save raw image for debugging
=
# Step 2: HSV-based green removal
=
# Step 3: Skip aggressive removal (disabled - causes speckles in subject)
# transparent_image = remove_green_screen_aggressive(...)
# Step 4: Clean up any semi-transparent edge artifacts
=
# Step 5: Save as PNG
return
Generate Stickers#
Let's generate some example stickers!
Python
=
=
| Raw (Green Screen) | Processed (Transparent) |
|---|---|
![]() |
![]() |
Python
=
=
=
| Raw (Green Screen) | Processed (Transparent) |
|---|---|
![]() |
![]() |
Prompt Engineering Tips#
- Always specify "sticker-style" or "illustration"
- Request "clear defined edges" for easier cutout
- Specify the background color explicitly
- Ask for the subject to be "centered with padding"
- Works best with subjects that don't contain green
Using Your Stickers#
The generated PNG files have proper alpha channels and can be used in:
- Design software (Figma, Photoshop, etc.)
- Presentation tools
- Chat applications
- Print-on-demand services
- Mobile apps
Thanks for reading! If you have any questions or feedback, please let me know on Twitter or LinkedIn.



