Any Image → Any Aspect Ratio
Upload any image and input your desired aspect ratio. Generate an outpainted version of your image to perfectly fit the new dimensions.
About
This ComfyUI workflow is designed to transform any image into a new aspect ratio by utilizing advanced outpainting techniques. At its core, the workflow leverages the Nano Banana 2 model, known for its robust image completion capabilities. The process begins by uploading an image and specifying the desired aspect ratio, such as 16:9, 9:16, 1:1, or 4:5.
The workflow then adapts the original image to fit the new dimensions, filling in the additional space with content that seamlessly extends the image's natural environment. This is achieved through a series of nodes, including LoadImage for importing the image, ImageCompositeMasked for handling the outpainting, and PreviewImage for visualizing the results.
What makes this workflow particularly useful is its ability to maintain the visual integrity of the original image while expanding its dimensions. This is ideal for content creators who need to adapt images for various platforms with different aspect ratio requirements. The integration of custom nodes like comfyui_essentials, controlaltai-nodes, and masquerade-nodes-comfyui further enhances the workflow's flexibility and precision, ensuring high-quality outputs that are both aesthetically pleasing and contextually relevant.
What you provide
- Reading the workflow
Runs in ComfyUI. This workflow can't run on this page. Open it in Comfy Cloud with everything loaded, or download it for your own ComfyUI.
Try in CloudInside the workflow
A read-only preview of the node graph. Open it in Comfy Cloud or download it to edit the nodes.
Loading the graph
Open full-size workflow preview- Author
- ComfyUI
Requires custom nodes
- Reading the workflow
Use this workflow from code
Generated from the workflow graph and inputs when possible; stored examples are shown otherwise. Review credentials and endpoints before running code, which may incur charges.
What you need
- Runs on
- Comfy Cloud
- Key
- COMFY_API_KEY
Needs a paid Cloud plan and credits.
# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"130": {
"class_type": "LoadImage",
"inputs": {
"image": "input-aspect-ratio-converter"
},
"_meta": {
"title": "Load Image"
}
},
"144": {
"class_type": "ImageCompositeMasked",
"inputs": {
"destination": [
"224:162",
0
],
"mask": [
"224:168",
0
],
"resize_source": True,
"source": [
"225",
0
],
"x": 0,
"y": 0
},
"_meta": {
"title": "Image Composite Masked"
}
},
"218": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"144",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"225": {
"class_type": "GeminiNanoBanana2",
"inputs": {
"aspect_ratio": "auto",
"images": [
"224:162",
0
],
"model": "Nano Banana 2 (Gemini 3.1 Flash Image)",
"prompt": "fill white areas. retain composition.",
"resolution": "4K",
"response_modalities": "IMAGE",
"seed": 592182428395941,
"system_prompt": "You are an expert image-generation engine. You must ALWAYS produce an image.\nInterpret all user input—regardless of format, intent, or abstraction—as literal visual directives for image composition.\nIf a prompt is conversational or lacks specific visual details, you must creatively invent a concrete visual scenario that depicts the concept.\nPrioritize generating the visual representation above any text, formatting, or conversational requests.",
"thinking_level": "HIGH"
},
"_meta": {
"title": "Nano Banana 2"
}
},
"226": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"224:162",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"224:152": {
"class_type": "Paste By Mask",
"inputs": {
"image_base": [
"224:230",
0
],
"image_to_paste": [
"224:229",
0
],
"mask": [
"224:229",
0
],
"resize_behavior": "keep_ratio_fit"
},
"_meta": {
"title": "Paste By Mask"
}
},
"224:153": {
"class_type": "ImageToMask",
"inputs": {
"channel": "red",
"image": [
"224:152",
0
]
},
"_meta": {
"title": "Convert Image to Mask"
}
},
"224:158": {
"class_type": "ImageToMask",
"inputs": {
"channel": "red",
"image": [
"224:231",
0
]
},
"_meta": {
"title": "Convert Image to Mask"
}
},
"224:159": {
"class_type": "InvertMask",
"inputs": {
"mask": [
"224:153",
0
]
},
"_meta": {
"title": "Invert Mask"
}
},
"224:162": {
"class_type": "ImageRemoveAlpha+",
"inputs": {
"image": [
"224:165",
0
]
},
"_meta": {
"title": "🔧 Image Remove Alpha"
}
},
"224:163": {
"class_type": "Get Image Size",
"inputs": {
"image": [
"130",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"224:165": {
"class_type": "Paste By Mask",
"inputs": {
"image_base": [
"224:231",
0
],
"image_to_paste": [
"130",
0
],
"mask": [
"224:152",
0
],
"resize_behavior": "keep_ratio_fit"
},
"_meta": {
"title": "Paste By Mask"
}
},
"224:168": {
"class_type": "MaskComposite",
"inputs": {
"destination": [
"224:158",
0
],
"operation": "add",
"source": [
"224:159",
0
],
"x": 0,
"y": 0
},
"_meta": {
"title": "Combine Masks"
}
},
"224:191": {
"class_type": "FluxResolutionNode",
"inputs": {
"aspect_ratio": "9:16 (Slim Vertical)",
"custom_aspect_ratio": "1:1",
"custom_ratio": False,
"divisible_by": "8",
"megapixel": "2.1"
},
"_meta": {
"title": "Flux Resolution Calc"
}
},
"224:229": {
"class_type": "LayerUtility: ColorImage V2",
"inputs": {
"color": "#ffffff",
"custom_height": [
"224:163",
1
],
"custom_width": [
"224:163",
0
],
"size": "custom"
},
"_meta": {
"title": "LayerUtility: ColorImage V2"
}
},
"224:230": {
"class_type": "LayerUtility: ColorImage V2",
"inputs": {
"color": "#000000",
"custom_height": [
"224:191",
1
],
"custom_width": [
"224:191",
0
],
"size": "custom"
},
"_meta": {
"title": "LayerUtility: ColorImage V2"
}
},
"224:231": {
"class_type": "LayerUtility: ColorImage V2",
"inputs": {
"color": "#ffffff",
"custom_height": [
"224:191",
1
],
"custom_width": [
"224:191",
0
],
"size": "custom"
},
"_meta": {
"title": "LayerUtility: ColorImage V2"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)Questions & answers
Yes, the workflow supports most common image formats, including JPEG, PNG, and BMP.
The Nano Banana 2 model is designed to blend seamlessly, but you can adjust parameters or manually refine the output if needed.
Yes, you can always reload the original image and start the process again if you're not satisfied with the results.
While the workflow can manage a wide range of aspect ratios, extremely disproportionate dimensions may require additional manual adjustments for optimal results.