A practical background-replacement prompt separates what to change, what to request unchanged, and how the new scene should connect to the subject. The official Gemini image-generation documentation demonstrates passing a text instruction and a base64-encoded source image in the same interaction. The recipe article recommends concrete instructions that describe what to change and what to preserve rather than a list of tags.
The target-preserve-integrate-constrain prompt
The background replacement recipe collection provides copyable prompts involving subject identity, pose, lighting, shadows, and product geometry. Those elements can be organized into a four-part editing brief: target, preserve, integrate, and constrain.
Replace only the background with [new environment]. Preserve [identity or product geometry] and [pose, framing, and other fixed details]. Match the lighting, color, shadows, and perspective of the new environment to the subject. Do not crop, add objects, or restyle the subject.
Here, “preserve” is an instruction to the editor, not a guarantee of an identical result. Inspect details that matter to the final image after every edit.
- Target: Name one replacement environment and limit the requested edit to the background.
- Preserve: List the identity, facial features, pose, clothing, product geometry, camera angle, framing, or other details that should remain fixed.
- Integrate: Specify the new light direction, color temperature, shadows, and perspective so the subject and background are visually connected.
- Constrain: State what the editor should not change, including crops, added objects, typography, or restyling.
How to run the editing workflow
- Inspect the source. Check hair, hands, transparent materials, product edges, existing lettering, pose, and framing before prompting.
- Build the instruction. Name the replacement environment, list the details to request unchanged, and describe the lighting and shadows to match.
- Choose an execution path. Use the documented Gemini interaction for the cloud workflow. For a supported offline alternative, use the matching recipe and its stated starting settings where applicable.
- Compare and refine. Compare the output with the source at full size. The official documentation describes multi-turn conversation as the recommended way to iterate on images.
Run the prompt through the Gemini image API
The following follows the Python input and output structure shown in the official Gemini image-generation example. The model identifier and API field names match the supplied example; the background-replacement wording is the prompt used for this workflow.
from google import genai
import base64
client = genai.Client()
with open('input.png', 'rb') as f:
image_data = f.read()
prompt = (
'Replace only the background with a bright modern studio. '
'Preserve the subject identity, face, expression, hairstyle, clothing, '
'pose, camera angle, and framing. Match the studio lighting, edge light, '
'and soft contact shadow to the subject. Do not change the subject.'
)
interaction = client.interactions.create(
model='gemini-nano-banana-2.1',
input=[
{'type': 'text', 'text': prompt},
{
'type': 'image',
'data': base64.b64encode(image_data).decode('utf-8'),
'mime_type': 'image/png'
}
]
)
with open('background-replaced.png', 'wb') as f:
f.write(base64.b64decode(interaction.output_image.data))
For a follow-up, the documentation uses previous_interaction_id to continue from an earlier interaction. A narrow correction can focus on the visible mismatch: “Keep the face, pose, framing, and clothing unchanged. Correct only the background edge and match the subject’s shadow to the new light direction.”
Cloud API and compatible open-model pipelines
The recipe article describes its discussed Nano Banana 2 workflow as closed and cloud-hosted. The prompt collection says its recipes can be used with a cloud client or supported offline alternatives. It separately identifies seed, steps, guidance, and strength as starting points for compatible open-model pipelines, not Gemini API parameters.
- Gemini API: Send the source image and text instruction together, then use a subsequent interaction if another edit is needed.
- Compatible open-model pipeline: The listed background recipe begins “replace background with neon cyberpunk city, preserve subject lighting and pose.” Its stated starting point is
seed=42,steps=30,guidance=4.0, andstrength=0.6. Do not add those pipeline parameters to the Gemini request.
Four background-related prompt recipes to adapt
1. Portrait or character identity
Change the background to a sunny beach. Keep the character identity and facial features, and preserve the pose. Match the lighting and shadows to the beach.
This combines the prompt collection’s identity-consistency example with its background-lighting instruction. State which character details must remain fixed before adding unrelated edit requests.
2. Product on a seamless background
Place the product on a white seamless background with a soft natural shadow. Preserve the product geometry. Do not redesign the product.
The prompt collection identifies this as a product-shadow recipe. Compare the product’s geometry and visible details with the source after generation rather than assuming the background change left them untouched.
3. AI image prompt for text and logos
Add a neon sign on the building wall that reads “CAFE OPEN 24H” in Art Deco style typography. Match the perspective and lighting of the scene. Keep the foreground subject unchanged.
The prompt collection includes this complex text-rendering example. If an edit involves a logo or other lettering, provide the exact visible string, style, and placement, then inspect every character. The supplied sources do not promise pixel-perfect logo or text reproduction.
4. Neon environment with matched lighting
Change the background to a rainy cyberpunk street at night. CRITICAL: Update the lighting on the subject’s face to match the neon blue and pink signs from the street.
The recipe article presents lighting consistency as important when changing backgrounds. For a wider composition, also inspect lighting and shadows on the body and around the subject’s edges.
Inspect common failure points
- Identity changes: Repeat the instruction to change only the environment and restate the relevant identity and facial locks.
- Lighting or edge mismatches: Name the new light direction and color, request matched lighting and shadows, and inspect the subject’s edges.
- Pose or crop changes: Restate the original pose, camera angle, framing, subject scale, and the instruction not to crop or restyle.
- Unwanted background objects: Use a direct command such as “Remove the crowd in the background. Replace them with empty chairs. Ensure the chairs cast shadows consistent with the sun.”
- Lettering differences: Quote the exact string, type style, and placement, then inspect every visible character rather than assuming the typography is exact.
Prompting describes the intended edit; it does not guarantee a perfect result. Compare the source and output for identity, geometry, edges, shadows, framing, lighting, and every visible character. Use the AI image prompt recipe collection for additional examples, the recipe article for lighting and typography examples, and the Nano Banana FAQ for related project information.