萌宝AI·图像约束
Get under the upload limit without eyeballing it
- image
- image
Every marketplace, print shop and CMS has a size cap, and AI output walks straight into it. A 4K transparent PNG off a cloud model is routinely 8–20 MB. The platform says 10 MB max, the uploader says "file too large", and you're back in Photoshop or hunting for an online compressor. MengBaoImageConstraint (萌宝AI·图像约束) does it in-graph: it fits the image inside a maximum width/height, and - the actual feature - inside a maximum file size, while preserving aspect ratio.
How the two constraints interact
The node computes a scale range from four integers: max_width/max_height (default 2048 each, 0 = unlimited) and min_width/min_height (default 0). If the image already sits inside that window, it's left alone - the minimums are a floor, not an upscale target. Note the step is 8, which is polite to latent-space pipelines.
When maximum and minimum are mutually exclusive - say a 1200px-wide image with min_width=2000 and max_width=1024 - there's an explicit tiebreak: maximum wins, and the image is just fitted to the box. crop_if_required (no by default) is the escape hatch. Set it to yes and the node switches to a cover-crop: scale up enough to satisfy the minimums, then center-crop to the target rectangle. That's how you get a guaranteed 1080x1350 out of a 4:3 source without it looking squashed.
The file-size part
max_file_size_mb defaults to 10 (0 disables the check), and the tooltip is the author's own summary: it's the maximum size of a single PNG encoded with 8-bit depth at 1 MB = 1024x1024 bytes, oversize images are scaled down proportionally, the size limit outranks the minimum dimensions, and workflow metadata appended by a save node isn't counted.
The implementation is more careful than a naive "shrink by ratio" pass. It actually encodes the PNG - twice, at compression level 4 and at the default level - takes the larger result, and only then decides. If it's over, it scales by min(0.95, sqrt(limit/actual) * 0.95), re-encodes, and loops until it fits or hits 1x1. It's slow on purpose: PNG size isn't linear in pixel count, so measuring beats predicting. Alpha is preserved throughout, and it re-encodes every frame in a batch, shrinking by whichever single image is biggest so the whole batch keeps one consistent size (the README calls this out explicitly).
Output is a single image.
Where it goes in the graph
Two placements. Right after generation, before anything that writes a file, is the simple one - image generator → constraint → save. The other is at the very end: MengBaoSmartCollage → constraint → save, which is the combination the README highlights, because a 4-up or 9-up collage is exactly the file that blows past a cap. If the platform also wants a fixed ratio, constraint → save with crop_if_required=yes handles both in one hop.
Installing it
It's one of 17 nodes in ComfyUI-MengBaoAI (Corkery520, MIT, Registry id mengbaoai). Manager search: MengBaoAI, mengbaoai, 萌宝AI; CLI: comfy node install mengbaoai. If the Registry release isn't live yet for you, Git:
cd ComfyUI/custom_nodes
git clone https://github.com/Corkery520/ComfyUI-MengBaoAI.git
cd ComfyUI-MengBaoAI
python -m pip install -r requirements.txt
numpy, Pillow, requests. Pillow is the one this node actually leans on - it's doing real PNG encodes - and ComfyUI already ships it, so the install is a no-op in most environments. Restart, then search 图像约束 or Constrain Image (both aliases are registered).
Things that bite
- The guarantee is about PNG bytes only. Encode the same pixels as JPEG or WebP and the size is different; the node has no opinion there. It also can't see the workflow metadata your save node tacks on afterwards, so if you're targeting 9.9 MB against a 10 MB cap, leave headroom.
- A tiny
min_widthwithmax_file_size_mbset produces a contradiction the node resolves silently - size limit first, minimums be damned. That's the documented precedence, not a bug, but it surprises people who set both. - Slow on big batches. Each iteration is a full PNG encode at two compression levels per frame. A 9-tile collage that needs four iterations is 72 encodes. Set a min_width floor you can live with rather than 1.
- It resizes, it doesn't resample for quality. Bilinear scaling is fine for a thumbnail-size reduction and noticeably soft if you're going from 4K to 1K. If that matters, upscale after constraining, or constrain the final deliverable only.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| max_width | INT | 20480–16384 | — |
| max_height | INT | 20480–16384 | — |
| min_width | INT | 00–16384 | — |
| min_height | INT | 00–16384 | — |
| crop_if_required | COMBO | no | 2 options: no, yes |
| max_file_size_mb | FLOAT | 10.00–1024 | 单张 PNG 最大体积(1 MB = 1024×1024 字节),0 为不限。超限时等比缩小,体积限制优先于最小尺寸;不包含保存节点追加的元数据。 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |