Calculate Kling expansion from the original canvas in a CometAPI request

A “10% wider” control can produce the wrong request if it treats each border as a percentage of the final canvas. In CometAPI’s Kling image-expansion endpoint, each side’s ratio refers to the original width or height. Left and right additions both count toward the new width; up and down both count toward the new height.

For an outpainting UI backed by CometAPI, configure the request builder for POST https://api.cometapi.com/kling/v1/images/editing/expand around the original canvas, not the preview size. Send left_expansion_ratio and right_expansion_ratio as 0.1 for 10% extra original width on each side, and explicitly send up_expansion_ratio and down_expansion_ratio as 0 to leave those borders unchanged. Validate all four fields against the documented 0–2 range before sending the request with its source image; do not treat that check as proof of acceptance, because CometAPI’s reference warns of a combined-area limit without giving its numerical ceiling.

What to send for unchanged borders

The CometAPI Kling expansion reference requires all four ratio fields. Each has a documented range of 0 through 2; an unchanged side is 0, not an omitted field.

For extra space on the left and right without adding space above or below, the ratio portion of the request body would be:

{
  "up_expansion_ratio": 0,
  "down_expansion_ratio": 0,
  "left_expansion_ratio": 0.1,
  "right_expansion_ratio": 0.1
}

This is only the ratio portion, not a complete request; the endpoint also requires the source image.

Preview the geometry from the original size

The following is arithmetic inferred from the documented ratio semantics, not a measured output guarantee. For original width W and height H:

nominal width  = W × (1 + left_expansion_ratio + right_expansion_ratio)
nominal height = H × (1 + up_expansion_ratio + down_expansion_ratio)

For a hypothetical 1000 × 800 source, the fields above add 100 pixels on each horizontal side: a nominal 1200 × 800 canvas. That is 20% more width overall, not 10%.

If all four ratios were 0.1, the nominal size would instead be 1200 × 960. Both dimensions grow by 20%, so the area becomes 1.44 times the original. Do not recalculate the right border from a width already expanded by the left border: both use the original 1000-pixel width.

A valid slider value is not a valid canvas

CometAPI’s page separately warns that the combined expanded canvas must remain within Kling’s documented area limits. It does not give a numerical total-area ceiling. Checking that every ratio lies within 0–2 therefore does not prove that the combination will be accepted.

For a size control, I would keep these checks separate:

  • Per-side inputs: serialize all four fields and check their documented ranges.
  • Geometry preview: show nominal width, height, and area growth from the original dimensions.
  • Combined-area validation: confirm the applicable ceiling before presenting a configuration as valid. Until that limit is established, label the preview as an estimate rather than an acceptance check.

The page links to Kling’s complete parameter reference, but no numerical cap is assumed here. Nor does this calculation establish output rounding, actual delivered dimensions, or how convincingly the new border content will match the source. It is a way to construct and explain CometAPI’s Kling expansion ratios without confusing border growth with final-canvas percentages.

The symmetric example is useful for explaining total growth, but it would not catch a request builder that swaps left and right. I would add an asymmetric, non-square fixture to the local geometry tests for this Kling adapter before testing generated images.

For a hypothetical 1000 × 800 input, use:

{
  "left_expansion_ratio": 0.2,
  "right_expansion_ratio": 0,
  "up_expansion_ratio": 0.1,
  "down_expansion_ratio": 0
}

Under the documented original-dimension semantics, the nominal preview is 1200 × 880, with 200 pixels added on the left and 80 above. In that local preview, the original image’s top-left corner should therefore sit at (200, 80). These are derived geometry expectations, not observations of Kling output.

Assert the serialized field names and explicit zeros as well as the preview dimensions and offset. A left/right swap preserves total width; a dimensions-only assertion would miss it. The non-square input also exposes using width where the vertical calculation needs height.

This tests the adapter’s interpretation without treating generated border quality as the oracle. Passing it would not establish server acceptance, exact delivered dimensions, or the unresolved combined-area ceiling—and these Kling field semantics should not be assumed for a different outpainting backend.