> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Mở rộng hình ảnh với Kling

> Sử dụng Kling Image Expansion API qua CometAPI để mở rộng hình ảnh vượt ra ngoài viền gốc với khả năng outpainting có thể kiểm soát.

Sử dụng endpoint này để outpaint một hình ảnh vượt ra ngoài các viền gốc của nó khi bạn cần thêm vùng canvas xung quanh nội dung nguồn.

## Cách các tỷ lệ mở rộng hoạt động

* Mỗi tỷ lệ được đo dựa trên chiều rộng hoặc chiều cao gốc
* Vùng canvas mở rộng kết hợp vẫn phải nằm trong giới hạn diện tích được Kling ghi rõ trong tài liệu
* Hãy bắt đầu với các giá trị nhỏ như `0.1` cho mỗi cạnh trước khi thử các mức mở rộng lớn hơn

## Luồng request

* Gửi cả bốn trường tỷ lệ, dùng `0` cho bất kỳ cạnh nào bạn không muốn mở rộng
* Lưu `task_id` được trả về
* Poll đường dẫn truy vấn hình ảnh Kling tương ứng cho đến khi task đạt trạng thái kết thúc

<Tip>
  Để xem tài liệu tham chiếu tham số đầy đủ, hãy xem [tài liệu Kling chính thức](https://kling.ai/document-api/api/image/common/outpainting).
</Tip>


## OpenAPI

````yaml api/openapi/image/kling/post-image-expansion.openapi.json POST /kling/v1/images/editing/expand
openapi: 3.1.0
info:
  title: Kling Image Expansion API
  version: 1.0.0
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /kling/v1/images/editing/expand:
    post:
      summary: Expand an image with Kling
      operationId: kling_image_expansion
      parameters:
        - name: Content-Type
          in: header
          required: false
          description: Must be `application/json`.
          schema:
            type: string
            default: application/json
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - image
                - up_expansion_ratio
                - down_expansion_ratio
                - left_expansion_ratio
                - right_expansion_ratio
              properties:
                image:
                  type: string
                  description: >-
                    Source image to expand. Image URL or raw Base64 string
                    without a `data:` prefix. Supported formats: JPG, JPEG, PNG.
                    Maximum 10 MB, minimum 300x300 px, aspect ratio between
                    1:2.5 and 2.5:1.
                up_expansion_ratio:
                  type: number
                  description: >-
                    Upward expansion ratio as a multiple of the original image
                    dimension. Range: 0-2. Send `0` for sides you do not want to
                    expand.
                  minimum: 0
                  maximum: 2
                down_expansion_ratio:
                  type: number
                  description: >-
                    Downward expansion ratio as a multiple of the original image
                    dimension. Range: 0-2. Send `0` for sides you do not want to
                    expand.
                  minimum: 0
                  maximum: 2
                left_expansion_ratio:
                  type: number
                  description: >-
                    Leftward expansion ratio as a multiple of the original image
                    dimension. Range: 0-2. Send `0` for sides you do not want to
                    expand.
                  minimum: 0
                  maximum: 2
                right_expansion_ratio:
                  type: number
                  description: >-
                    Rightward expansion ratio as a multiple of the original
                    image dimension. Range: 0-2. Send `0` for sides you do not
                    want to expand.
                  minimum: 0
                  maximum: 2
                prompt:
                  type: string
                  description: >-
                    Optional text prompt to guide the expanded area content.
                    Maximum 2500 characters.
                'n':
                  type: integer
                  description: 'Number of expanded images to generate. Range: 1-9.'
                  minimum: 1
                  maximum: 9
                  default: 1
                watermark_info:
                  type: object
                  description: Watermark options.
                  properties:
                    enabled:
                      type: boolean
                callback_url:
                  type: string
                  description: Webhook URL for task status notifications.
                external_task_id:
                  type: string
                  description: >-
                    Optional user-defined task ID for your own tracking. Must be
                    unique per account.
              default:
                image: https://your-image-host/source.jpg
                up_expansion_ratio: 0.1
                down_expansion_ratio: 0.1
                left_expansion_ratio: 0.1
                right_expansion_ratio: 0.1
                prompt: extend the soft studio background
                'n': 1
            examples:
              Default:
                summary: Expand an image on all four sides
                value:
                  image: https://your-image-host/source.jpg
                  up_expansion_ratio: 0.1
                  down_expansion_ratio: 0.1
                  left_expansion_ratio: 0.1
                  right_expansion_ratio: 0.1
                  prompt: extend the soft studio background
                  'n': 1
      responses:
        '200':
          description: Task request accepted or an error response returned by the API.
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                  - data
                properties:
                  code:
                    oneOf:
                      - type: integer
                      - type: string
                    description: Response code. `0` means the task request was accepted.
                  message:
                    type: string
                    description: Response message.
                  request_id:
                    type: string
                    description: Request identifier returned when present.
                  data:
                    type: object
                    required:
                      - task_id
                      - task_status
                      - created_at
                      - updated_at
                    properties:
                      task_id:
                        type: string
                        description: System-generated task ID.
                      task_status:
                        type: string
                        description: Task status.
                        enum:
                          - submitted
                          - processing
                          - succeed
                          - failed
                      task_status_msg:
                        oneOf:
                          - type: string
                          - type: 'null'
                        description: Task status detail when present.
                      task_info:
                        type: object
                        properties:
                          external_task_id:
                            oneOf:
                              - type: string
                              - type: 'null'
                            description: Caller-provided external task ID when supplied.
                        additionalProperties: true
                      task_result:
                        oneOf:
                          - type: object
                            additionalProperties: true
                          - type: 'null'
                        description: Task result payload after the task completes.
                      created_at:
                        type: integer
                        description: >-
                          Task creation time as a Unix timestamp in
                          milliseconds.
                      updated_at:
                        type: integer
                        description: Task update time as a Unix timestamp in milliseconds.
                    additionalProperties: true
                additionalProperties: true
      x-codeSamples:
        - lang: Shell
          label: Default
          source: |
            curl https://api.cometapi.com/kling/v1/images/editing/expand \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
              "image": "https://your-image-host/source.jpg",
              "up_expansion_ratio": 0.1,
              "down_expansion_ratio": 0.1,
              "left_expansion_ratio": 0.1,
              "right_expansion_ratio": 0.1,
              "prompt": "extend the soft studio background",
              "n": 1
            }'
        - lang: Python
          label: Default
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/kling/v1/images/editing/expand",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                "image": "https://your-image-host/source.jpg",
                "up_expansion_ratio": 0.1,
                "down_expansion_ratio": 0.1,
                "left_expansion_ratio": 0.1,
                "right_expansion_ratio": 0.1,
                "prompt": "extend the soft studio background",
                "n": 1
            },
            )

            result = response.json()
            print(result["code"], result.get("data", {}).get("task_id"))
        - lang: JavaScript
          label: Default
          source: >
            const response = await
            fetch("https://api.cometapi.com/kling/v1/images/editing/expand", {
              method: "POST",
              headers: {
                Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                "Content-Type": "application/json",
              },
              body: JSON.stringify({
                "image": "https://your-image-host/source.jpg",
                "up_expansion_ratio": 0.1,
                "down_expansion_ratio": 0.1,
                "left_expansion_ratio": 0.1,
                "right_expansion_ratio": 0.1,
                "prompt": "extend the soft studio background",
                "n": 1
            }),

            });


            const result = await response.json();

            console.log(result.code, result.data?.task_id);
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````