{"record":{"id":"e49b3d5452491e29","repo":"unslothai/unsloth","slug":"upscale-would-not-enlarge-this-image-its-longest","errorCode":null,"errorMessage":"Upscale would not enlarge this image: its longest side ({max(iw, ih)}px) already meets the {max_side}px output limit. Use a smaller source image.","messagePattern":"Upscale would not enlarge this image: its longest side \\((.+?)px\\) already meets the (.+?)px output limit\\. Use a smaller source image\\.","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"studio/backend/core/inference/diffusion.py","lineNumber":5388,"sourceCode":"                    pipe = self._workflow_pipe(state, state.family.inpaint_pipeline_class, workflow)\n                    init_pil = decode_b64_image(init_image, mode = \"RGB\")\n                    mask_pil = decode_b64_image(mask_image, mode = \"L\")\n                elif init_image is not None and upscale is not None and upscale > 1.0:\n                    # Upscale (hires fix): enlarge with Lanczos, then re-run img2img at low strength to add detail.\n                    workflow = \"upscale\"\n                    pipe = self._workflow_pipe(state, state.family.img2img_pipeline_class, workflow)\n                    init_pil = decode_b64_image(init_image, mode = \"RGB\")\n                    iw, ih = init_pil.size\n                    # Cap the factor, then the absolute output (longest side 2048); round to a multiple of 16 (VAE downsample + patch).\n                    factor = max(1.0, min(float(upscale), 4.0))\n                    tw_f, th_f = iw * factor, ih * factor\n                    max_side = 2048\n                    fit = min(1.0, max_side / max(tw_f, th_f))\n                    tw = max(16, int(round(tw_f * fit / 16.0)) * 16)\n                    th = max(16, int(round(th_f * fit / 16.0)) * 16)\n                    # After the cap, the target must still exceed the input (else upscale shrinks it).\n                    if max(tw, th) <= max(iw, ih):\n                        raise ValueError(\n                            f\"Upscale would not enlarge this image: its longest side \"\n                            f\"({max(iw, ih)}px) already meets the {max_side}px output limit. \"\n                            f\"Use a smaller source image.\"\n                        )\n                    init_pil = init_pil.resize((tw, th), Image.LANCZOS)\n                    if strength is None:\n                        strength = 0.35  # hires-fix default: preserve content, add detail\n                elif getattr(state.family, \"reference\", False) and init_image is not None:\n                    # FLUX.2 reference conditioning: the loaded pipe takes the reference via `image` and generates at the REQUESTED size.\n                    workflow = \"reference\"\n                    init_pil = decode_b64_image(init_image, mode = \"RGB\")\n                    # Additional references (FLUX.2 combines a list); capped to bound VRAM.\n                    ref_extra = [\n                        decode_b64_image(x, mode = \"RGB\") for x in (reference_images or [])[:3]\n                    ]\n                elif init_image is not None:\n                    workflow = \"img2img\"\n                    pipe = self._workflow_pipe(state, state.family.img2img_pipeline_class, workflow)","sourceCodeStart":5370,"sourceCodeEnd":5406,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/core/inference/diffusion.py#L5370-L5406","documentation":"In the upscale (hires-fix) workflow, the target size is computed as the input size times a factor clamped to [1.0, 4.0], then capped so the longest side does not exceed 2048px and snapped to a multiple of 16. If the source image's longest side already meets or exceeds the capped target, the 'upscale' would actually shrink or no-op, so the code refuses with this ValueError instead of returning a smaller image.","triggerScenarios":"Calling generate with init_image set, upscale > 1.0, and an input image whose max(iw, ih) is at or above 2048px (or close enough that the 16px rounding lands the target at or below the input). E.g. a 2048x1536 photo with upscale=2 would target 4096 -> capped to 2048 -> not larger than input.","commonSituations":"Feeding full-resolution phone photos (commonly 4000px+) into an img2img upscale endpoint; re-running upscale on an already-upscaled output that hit the 2048 cap.","solutions":["Downscale the source image so its longest side is comfortably below 2048px before requesting upscale (e.g. resize to 1024-1536px longest side).","Skip the upscale flag and use plain img2img at the source resolution if enlargement is not needed.","Raise the max_side cap in your own fork only if you have VRAM/headroom for larger outputs — stock builds cap at 2048."],"exampleFix":"# before\nengine.generate(prompt=..., init_image=big_2048px_b64, upscale=2.0)\n# after\nfrom PIL import Image\nimg = decode(b64); img.thumbnail((1536, 1536))\nengine.generate(prompt=..., init_image=encode_b64(img), upscale=2.0)  # 1536 -> ~3072 capped to 2048, still larger","handlingStrategy":"validation","validationCode":"from PIL import Image\nimport io\n\nMAX_SIDE = 2048\n\ndef upscale_viable(img_b64: str, upscale: float) -> bool:\n    img = Image.open(io.BytesIO(decode(img_b64)))\n    factor = max(1.0, min(float(upscale), 4.0))\n    tw, th = img.width * factor, img.height * factor\n    fit = min(1.0, MAX_SIDE / max(tw, th))\n    tw = max(16, int(round(tw * fit / 16.0)) * 16)\n    th = max(16, int(round(th * fit / 16.0)) * 16)\n    return max(tw, th) > max(img.size)","typeGuard":null,"tryCatchPattern":"try:\n    out = engine.generate(prompt=p, init_image=img_b64, upscale=2.0)\nexcept ValueError as e:\n    if \"would not enlarge\" in str(e):\n        out = engine.generate(prompt=p, init_image=downscale_b64(img_b64, longest=1536), upscale=2.0)\n    else:\n        raise","preventionTips":["Pre-resize uploads so the longest side is 1024-1536px before requesting upscale.","Do not chain upscale calls on outputs already at the 2048 cap."],"tags":["diffusion","upscale","validation","image-size"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}