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.
简介
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.
你需要提供
- 正在读取工作流
在 ComfyUI 中运行。 此工作流无法在本页面运行。你可以在 Comfy Cloud 中打开(已预先加载所需内容),或下载后在自己的 ComfyUI 中使用。
在 Cloud 中试用查看工作流内部
节点图的只读预览。在 Comfy Cloud 中打开或下载后即可编辑节点。
Loading the graph
Open full-size workflow preview- 作者
- ComfyUI
需要自定义节点
- 正在读取工作流
通过代码使用此工作流
尽可能根据工作流图和输入自动生成代码,否则显示已保存的示例。运行代码可能产生费用,请先核对凭据和端点。
What you need
- Runs on
- Comfy Cloud
- 密钥
- COMFY_API_KEY
需要付费的 Cloud 方案和额度。
# 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)常见问答
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.