> ## Documentation Index
> Fetch the complete documentation index at: https://docs-docflow.textin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 複数画像クロップ

> 1ページに複数の独立した画像を含む文書をインテリジェントに認識・切り取りし、経費精算の領収書貼り付けなどのシーンに対応

## 機能概要

複数画像クロップ機能は、文書の 1 ページに含まれる複数の独立した画像やチケットを自動的に認識し、それぞれ独立したサブ文書として切り取ります。経費精算の添付書類や複数の領収書を含むスキャン画像の処理に特に有用です。

## 利用シーン

### 1. 経費精算の領収書貼り付けシーン

A4 用紙に複数のチケットを平置きした場合：

* 鉄道切符
* 航空券明細
* 複数のタクシー領収書
* 飲食店の領収書

複数画像クロップ機能により、各チケットを個別に認識・切り取りし、後続の分類と金額抽出を容易にします。

<img src="https://mintcdn.com/docflow-bbb41569/Hl8q91UUG5hgv3co/docflow-global/ja/images/crop_example.jpg?fit=max&auto=format&n=Hl8q91UUG5hgv3co&q=85&s=c9ceb46482ba8f0608d79c2a791e9b11" alt="crop example" width="2554" height="3659" data-path="docflow-global/ja/images/crop_example.jpg" />

## API パラメータ設定

### 複数画像クロップ機能を有効化

アップロード API で `crop_flag=true` を設定し、複数画像クロップ機能を有効にします。

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -F "file=@/path/to/multi-image-document.pdf" \
    "https://docflow.textin.ai/api/app-api/sip/platform/v2/file/upload?workspace_id=<your-workspace-id>&crop_flag=true"
  ```
</CodeGroup>

### パラメータ説明

| パラメータ名      | 型       | デフォルト値 | 説明                   |
| ----------- | ------- | ------ | -------------------- |
| `crop_flag` | boolean | false  | 複数画像クロップ機能を有効にするかどうか |

## サンプルコード

<CodeGroup>
  ```python Python expandable icon=python theme={null}
  import requests
  import json

  def upload_with_crop(file_path, workspace_id, app_id, secret_code):
      """
      Upload file and enable multi-image cropping function
      """
      url = "https://docflow.textin.ai/api/app-api/sip/platform/v2/file/upload"

      headers = {
          "x-ti-app-id": app_id,
          "x-ti-secret-code": secret_code
      }

      params = {
          "workspace_id": workspace_id,
          "crop_flag": "true"  # Enable multi-image cropping function
      }

      with open(file_path, 'rb') as file:
          files = {'file': file}
          response = requests.post(url, headers=headers, params=params, files=files)

      return response.json()

  def fetch_crop_results(workspace_id, batch_number, app_id, secret_code):
      """
      Query multi-image cropping results
      """
      url = "https://docflow.textin.ai/api/app-api/sip/platform/v2/file/fetch"

      headers = {
          "x-ti-app-id": app_id,
          "x-ti-secret-code": secret_code
      }

      params = {
          "workspace_id": workspace_id,
          "batch_number": batch_number
      }

      response = requests.get(url, headers=headers, params=params)
      return response.json()

  def parse_crop_coordinates(from_parent_position_list):
      """
      Parse cropping coordinate information
      Coordinate format: [x1, y1, x2, y2, x3, y3, x4, y4]
      Represents the four vertex coordinates of a rectangle
      """
      if len(from_parent_position_list) != 8:
          return None

      coordinates = {
          "top_left": (from_parent_position_list[0], from_parent_position_list[1]),
          "top_right": (from_parent_position_list[2], from_parent_position_list[3]),
          "bottom_right": (from_parent_position_list[4], from_parent_position_list[5]),
          "bottom_left": (from_parent_position_list[6], from_parent_position_list[7])
      }

      # Calculate bounding box
      x_coords = [coord[0] for coord in coordinates.values()]
      y_coords = [coord[1] for coord in coordinates.values()]

      bbox = {
          "x_min": min(x_coords),
          "y_min": min(y_coords),
          "x_max": max(x_coords),
          "y_max": max(y_coords),
          "width": max(x_coords) - min(x_coords),
          "height": max(y_coords) - min(y_coords)
      }

      return {"coordinates": coordinates, "bbox": bbox}

  # Usage example
  if __name__ == "__main__":
      # Configuration information
      WORKSPACE_ID = "your-workspace-id"
      APP_ID = "your-app-id"
      SECRET_CODE = "your-secret-code"
      FILE_PATH = "/path/to/multi-image-document.pdf"

      # Upload file and enable multi-image cropping
      upload_result = upload_with_crop(FILE_PATH, WORKSPACE_ID, APP_ID, SECRET_CODE)
      print("Upload result:", json.dumps(upload_result, indent=2, ensure_ascii=False))

      # Get batch number
      batch_number = upload_result.get("result", {}).get("batch_number")

      if batch_number:
          # Query multi-image cropping results
          fetch_result = fetch_crop_results(WORKSPACE_ID, batch_number, APP_ID, SECRET_CODE)
          print("Multi-image cropping result:", json.dumps(fetch_result, indent=2, ensure_ascii=False))

          # Parse coordinate information
          files = fetch_result.get("result", {}).get("files", [])
          for file in files:
              child_files = file.get("child_files", [])
              for child in child_files:
                  if child.get("task_type") == 3:  # Sub-file generated by multi-image cropping
                      position_list = child.get("from_parent_position_list")
                      if position_list:
                          coord_info = parse_crop_coordinates(position_list)
                          print(f"Coordinate information for sub-file {child.get('name')}:", coord_info)
  ```
</CodeGroup>

## 結果の説明

### 複数画像クロップの結果構造

複数画像クロップ機能を有効にした場合、`file/fetch` API のレスポンスに `child_files` フィールドが含まれ、クロップ後のサブ文書の情報が記録されます。

```json expandable theme={null}
{
  "code": 200,
  "result": {
    "files": [
      {
        "id": "parent-file-001",
        "name": "multi-image-document.pdf",
        "format": "pdf",
        "child_files": [
          {
            "id": "child-001",
            "task_id": "task-001",
            "task_type": 3,  // 3 indicates sub-file generated by multi-image cropping
            "name": "multi-image-document.pdf#1",
            "format": "pdf",
            "category": "invoice",
            "from_parent_position_list": [12, 30, 420, 30, 420, 320, 12, 320],
            "crop_info":{"page":0,"imageAngle":"0"}
            "status": "success"
          },
          {
            "id": "child-002",
            "task_id": "task-002",
            "task_type": 3,
            "name": "multi-image-document.pdf#2",
            "format": "pdf",
            "category": "receipt",
            "from_parent_position_list": [450, 30, 800, 30, 800, 200, 450, 200],
            "crop_info":{"page":0,"imageAngle":"0"}
            "status": "success"
          }
        ]
      }
    ]
  }
}
```

### 主要フィールドの説明

| フィールド名                                    | 型       | 説明                                                    |
| ----------------------------------------- | ------- | ----------------------------------------------------- |
| `child_files`                             | array   | クロップ後のサブファイルリスト                                       |
| `child_files[].id`                        | string  | サブファイルの一意識別子                                          |
| `child_files[].task_type`                 | integer | タスクタイプ。3 は複数画像クロップによって生成されたことを示す                      |
| `child_files[].category`                  | string  | 文書分類結果                                                |
| `child_files[].from_parent_position_list` | array   | 元画像におけるクロップ領域の座標。[座標系の説明](../03-parse/coordinate) を参照 |
| `child_files[].crop_info`                 | object  | 複数画像クロップの詳細情報。ページインデックスと角度情報を含む                       |
