{"product_id":"crop-tool-cho-claude-khả-nang-zoom-vao-chi-tiết-hinh-ảnh","title":"Crop Tool — Cho Claude khả năng zoom vào chi tiết hình ảnh","description":"\n\u003cp\u003eMột trong những giới hạn của Vision AI là độ phân giải hiệu dụng — khi gửi ảnh toàn cảnh lớn, các chi tiết nhỏ bị mất đi. Giải pháp? \u003cstrong\u003eCho Claude một công cụ crop\u003c\/strong\u003e — để nó tự xác định vùng cần xem chi tiết và yêu cầu bạn gửi ảnh đã cắt xén.\u003c\/p\u003e\n\n\u003cp\u003ePattern này từ Anthropic Cookbook giúp Claude phân tích tài liệu phức tạp, bản vẽ kỹ thuật, hay bất kỳ ảnh nào có chi tiết quan trọng ở nhiều vùng khác nhau.\u003c\/p\u003e\n\n\u003ch2\u003eÝ tưởng cốt lõi\u003c\/h2\u003e\n\n\u003cp\u003eLuồng hoạt động như sau:\u003c\/p\u003e\n\n\u003col\u003e\n  \u003cli\u003e\n\u003cstrong\u003eGửi ảnh gốc\u003c\/strong\u003e — Client gửi ảnh toàn cảnh cho Claude\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eClaude nhận diện vùng cần xem kỹ\u003c\/strong\u003e — Claude dùng crop tool để \"yêu cầu\" cắt vùng cụ thể\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eClient crop và gửi lại\u003c\/strong\u003e — Bạn cắt vùng đó, gửi ảnh cắt xén cho Claude\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eClaude phân tích chi tiết\u003c\/strong\u003e — Với ảnh đã được zoom in, Claude đọc được thông tin chính xác hơn\u003c\/li\u003e\n\u003c\/ol\u003e\n\n\u003cp\u003eĐây là ví dụ điển hình của \u003cstrong\u003eTool Use pattern với Vision\u003c\/strong\u003e — Claude không chỉ nhìn thụ động mà chủ động điều khiển quá trình phân tích.\u003c\/p\u003e\n\n\u003ch2\u003eĐịnh nghĩa Crop Tool\u003c\/h2\u003e\n\n\u003cp\u003eĐầu tiên, định nghĩa tool mà Claude có thể gọi:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003eCROP_TOOL = {\n    \"name\": \"crop_image\",\n    \"description\": \"\"\"Cat mot vung cua hinh anh hien tai de xem chi tiet hon.\n    Dung khi ban can doc van ban nho, xem chi tiet ky thuat,\n    hoac phan tich mot vung cu the ma khong ro o do phan giai hien tai.\"\"\",\n    \"input_schema\": {\n        \"type\": \"object\",\n        \"properties\": {\n            \"left\": {\n                \"type\": \"number\",\n                \"description\": \"Toa do x cua goc trai tren (0-100, tinh theo %, relative den chieu rong anh)\"\n            },\n            \"top\": {\n                \"type\": \"number\",\n                \"description\": \"Toa do y cua goc trai tren (0-100, tinh theo %, relative den chieu cao anh)\"\n            },\n            \"right\": {\n                \"type\": \"number\",\n                \"description\": \"Toa do x cua goc phai duoi (0-100, tinh theo %)\"\n            },\n            \"bottom\": {\n                \"type\": \"number\",\n                \"description\": \"Toa do y cua goc phai duoi (0-100, tinh theo %)\"\n            },\n            \"reason\": {\n                \"type\": \"string\",\n                \"description\": \"Ly do tai sao can xem vung nay chi tiet hon\"\n            }\n        },\n        \"required\": [\"left\", \"top\", \"right\", \"bottom\", \"reason\"]\n    }\n}\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003cp\u003eTọa độ dùng đơn vị \u003cstrong\u003ephần trăm (0-100)\u003c\/strong\u003e thay vì pixel tuyệt đối — nhờ đó hoạt động với ảnh mọi kích thước mà không cần Claude biết kích thước pixel chính xác.\u003c\/p\u003e\n\n\u003ch2\u003eHàm crop thực tế\u003c\/h2\u003e\n\n\u003cpre\u003e\u003ccode\u003efrom PIL import Image\nimport io, base64\n\ndef crop_image(\n    original_image: bytes,\n    left: float,\n    top: float,\n    right: float,\n    bottom: float\n) -\u0026gt; tuple:\n    \"\"\"\n    Cat anh theo toa do phan tram.\n\n    Args:\n        original_image: Raw bytes cua anh goc\n        left, top, right, bottom: Toa do phan tram (0-100)\n\n    Returns:\n        (base64_string, media_type)\n    \"\"\"\n    img = Image.open(io.BytesIO(original_image))\n    width, height = img.size\n\n    # Chuyen phan tram sang pixel\n    x1 = int(width * left \/ 100)\n    y1 = int(height * top \/ 100)\n    x2 = int(width * right \/ 100)\n    y2 = int(height * bottom \/ 100)\n\n    # Dam bao bounds hop le\n    x1 = max(0, min(x1, width))\n    y1 = max(0, min(y1, height))\n    x2 = max(x1 + 1, min(x2, width))\n    y2 = max(y1 + 1, min(y2, height))\n\n    # Cat va encode\n    cropped = img.crop((x1, y1, x2, y2))\n    buf = io.BytesIO()\n    fmt = \"PNG\" if img.mode == \"RGBA\" else \"JPEG\"\n    cropped.save(buf, format=fmt, quality=90)\n    data = base64.standard_b64encode(buf.getvalue()).decode()\n    media_type = \"image\/png\" if fmt == \"PNG\" else \"image\/jpeg\"\n\n    return data, media_type\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eVòng lặp agentic chính\u003c\/h2\u003e\n\n\u003cp\u003eĐây là phần quan trọng nhất — vòng lặp xử lý tool calls:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003eimport anthropic\n\ndef analyze_with_crop_tool(\n    image_path: str,\n    question: str,\n    max_iterations: int = 5\n) -\u0026gt; str:\n    \"\"\"\n    Phan tich anh, cho phep Claude tu request crop khi can.\n\n    Args:\n        image_path: Duong dan den file anh\n        question: Cau hoi ve hinh anh\n        max_iterations: So lan crop toi da cho phep\n    \"\"\"\n    client = anthropic.Anthropic()\n\n    # Doc anh goc\n    with open(image_path, \"rb\") as f:\n        original_bytes = f.read()\n\n    image_data = base64.standard_b64encode(original_bytes).decode()\n    media_type = \"image\/jpeg\"  # Hoac detect tu extension\n\n    # Tin nhan ban dau\n    messages = [\n        {\n            \"role\": \"user\",\n            \"content\": [\n                {\n                    \"type\": \"text\",\n                    \"text\": f\"\"\"Ban co the su dung cong cu crop_image de xem chi tiet\n                    bat ky vung nao trong anh. Hay su dung no neu can thiet.\n\n                    Cau hoi: {question}\"\"\"\n                },\n                {\n                    \"type\": \"image\",\n                    \"source\": {\n                        \"type\": \"base64\",\n                        \"media_type\": media_type,\n                        \"data\": image_data\n                    }\n                }\n            ]\n        }\n    ]\n\n    # Vong lap xu ly tool calls\n    for iteration in range(max_iterations):\n        response = client.messages.create(\n            model=\"claude-opus-4-5\",\n            max_tokens=4096,\n            tools=[CROP_TOOL],\n            messages=messages\n        )\n\n        # Kiem tra stop reason\n        if response.stop_reason == \"end_turn\":\n            # Claude hoan thanh, tra ve ket qua cuoi\n            for block in response.content:\n                if hasattr(block, \"text\"):\n                    return block.text\n            break\n\n        if response.stop_reason == \"tool_use\":\n            # Tim tool call trong response\n            tool_calls = [\n                b for b in response.content\n                if b.type == \"tool_use\"\n            ]\n\n            if not tool_calls:\n                break\n\n            # Them assistant message vao lich su\n            messages.append({\n                \"role\": \"assistant\",\n                \"content\": response.content\n            })\n\n            # Xu ly tung tool call\n            tool_results = []\n            for tool_call in tool_calls:\n                args = tool_call.input\n                print(f\"Claude dang crop vung: {args['left']:.0f}%-{args['right']:.0f}% x {args['top']:.0f}%-{args['bottom']:.0f}%\")\n                print(f\"Ly do: {args.get('reason', '')}\")\n\n                # Thuc hien crop\n                crop_data, crop_media_type = crop_image(\n                    original_bytes,\n                    args[\"left\"], args[\"top\"],\n                    args[\"right\"], args[\"bottom\"]\n                )\n\n                # Tra ve ket qua crop cho Claude\n                tool_results.append({\n                    \"type\": \"tool_result\",\n                    \"tool_use_id\": tool_call.id,\n                    \"content\": [\n                        {\n                            \"type\": \"text\",\n                            \"text\": f\"Day la vung anh da duoc cat ({args['left']:.0f}%-{args['right']:.0f}% x {args['top']:.0f}%-{args['bottom']:.0f}%):\"\n                        },\n                        {\n                            \"type\": \"image\",\n                            \"source\": {\n                                \"type\": \"base64\",\n                                \"media_type\": crop_media_type,\n                                \"data\": crop_data\n                            }\n                        }\n                    ]\n                })\n\n            # Them tool results vao lich su\n            messages.append({\n                \"role\": \"user\",\n                \"content\": tool_results\n            })\n\n    return \"Khong the hoan thanh phan tich.\"\n\n\n# Chay thu\nif __name__ == \"__main__\":\n    result = analyze_with_crop_tool(\n        \"document.png\",\n        \"Doc toan bo noi dung cua tai lieu nay va tom tat.\"\n    )\n    print(result)\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eVí dụ thực tế: Phân tích hóa đơn phức tạp\u003c\/h2\u003e\n\n\u003cp\u003eHóa đơn thường có nhiều vùng thông tin: header, line items, tổng cộng, điều khoản. Crop tool giúp Claude đọc từng vùng chính xác hơn:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003edef extract_invoice_data(invoice_path: str) -\u0026gt; dict:\n    \"\"\"\n    Trich xuat du lieu hoa don bang crop tool.\n    \"\"\"\n    question = \"\"\"Trich xuat thong tin tu hoa don nay:\n    1. So hoa don va ngay\n    2. Thong tin nha cung cap (ten, dia chi)\n    3. Thong tin khach hang (ten, dia chi)\n    4. Danh sach san pham\/dich vu va gia\n    5. Tong tien (chua thue va co thue)\n    6. Hanh thuc va thoi han thanh toan\n\n    Su dung crop tool de doc ro tung phan neu can.\n    Tra ve du lieu dang JSON.\"\"\"\n\n    result = analyze_with_crop_tool(invoice_path, question)\n    return result\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eVí dụ: Đọc bản vẽ kỹ thuật\u003c\/h2\u003e\n\n\u003cpre\u003e\u003ccode\u003edef read_technical_drawing(drawing_path: str) -\u0026gt; str:\n    \"\"\"\n    Doc thong so ky thuat tu ban ve.\n    \"\"\"\n    question = \"\"\"Day la ban ve ky thuat. Hay:\n    1. Tim va doc title block (goc phai duoi thuong co thong tin ve ban ve)\n    2. Doc cac kich thuoc chinh\n    3. Tim cac ghi chu quan trong\n    4. Mo ta hinh dang tong the cua chi tiet\n\n    Dung crop_image de xem chi tiet cac so, ky hieu kho doc.\"\"\"\n\n    return analyze_with_crop_tool(\n        drawing_path,\n        question,\n        max_iterations=8  # Ban ve ky thuat co the can nhieu lan crop\n    )\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eTối ưu và giới hạn\u003c\/h2\u003e\n\n\u003ch3\u003eGiới hạn số lần crop\u003c\/h3\u003e\n\n\u003cp\u003eMỗi lần crop tốn thêm một API call. Giới hạn \u003ccode\u003emax_iterations\u003c\/code\u003e hợp lý để kiểm soát chi phí:\u003c\/p\u003e\n\n\u003cul\u003e\n  \u003cli\u003eTài liệu đơn giản: 3-4 lần crop là đủ\u003c\/li\u003e\n  \u003cli\u003eTài liệu phức tạp (hóa đơn, hợp đồng): 5-8 lần\u003c\/li\u003e\n  \u003cli\u003eBản vẽ kỹ thuật, bản đồ: 8-15 lần\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003ch3\u003eKích thước crop tối thiểu\u003c\/h3\u003e\n\n\u003cp\u003eThêm validation để tránh crop quá nhỏ (vô nghĩa) hoặc quá lớn (không zoom được):\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003edef validate_crop_args(args: dict) -\u0026gt; bool:\n    \"\"\"Kiem tra toa do crop hop le.\"\"\"\n    left, top = args[\"left\"], args[\"top\"]\n    right, bottom = args[\"right\"], args[\"bottom\"]\n\n    # Dam bao vung cat it nhat 5% theo moi chieu\n    if (right - left) \u0026lt; 5 or (bottom - top) \u0026lt; 5:\n        return False\n\n    # Dam bao khong vuot ngoai anh\n    if left \u0026lt; 0 or top \u0026lt; 0 or right \u0026gt; 100 or bottom \u0026gt; 100:\n        return False\n\n    return True\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch3\u003eCache crop results\u003c\/h3\u003e\n\n\u003cp\u003eNếu Claude crop cùng vùng nhiều lần (hiếm nhưng có thể xảy ra), cache kết quả để tránh xử lý lại:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003ecrop_cache = {}\n\ndef crop_image_cached(original_bytes, left, top, right, bottom):\n    key = (left, top, right, bottom)\n    if key not in crop_cache:\n        crop_cache[key] = crop_image(original_bytes, left, top, right, bottom)\n    return crop_cache[key]\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eKhi nào nên dùng Crop Tool?\u003c\/h2\u003e\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n\u003cth\u003eNên dùng\u003c\/th\u003e\n\u003cth\u003eKhông cần dùng\u003c\/th\u003e\n\u003c\/tr\u003e\n  \u003c\/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n\u003ctd\u003eTài liệu nhiều trang, nhiều section\u003c\/td\u003e\n\u003ctd\u003eẢnh đơn giản, ít thông tin\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eHóa đơn, hợp đồng, form scan\u003c\/td\u003e\n\u003ctd\u003eNhận diện đối tượng tổng thể\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eBản vẽ kỹ thuật, bản đồ chi tiết\u003c\/td\u003e\n\u003ctd\u003ePhân tích cảm xúc, tông màu\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eSlide deck với text nhỏ\u003c\/td\u003e\n\u003ctd\u003eẢnh chụp đơn giản\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eText nhỏ khó đọc ở nhiều vùng\u003c\/td\u003e\n\u003ctd\u003eKhi ảnh đã đủ độ phân giải\u003c\/td\u003e\n\u003c\/tr\u003e\n  \u003c\/tbody\u003e\n\u003c\/table\u003e\n\n\u003ch2\u003eTổng kết\u003c\/h2\u003e\n\n\u003cp\u003eCrop Tool Pattern là một trong những kỹ thuật mạnh nhất cho Vision tasks phức tạp. Thay vì bị giới hạn bởi độ phân giải của ảnh gốc, Claude có thể \u003cstrong\u003echủ động điều hướng phân tích\u003c\/strong\u003e — giống như con người dùng kính lúp để xem chi tiết.\u003c\/p\u003e\n\n\u003cp\u003eCode hoàn chỉnh của pattern này có trên \u003ca href=\"\/en\/collections\/nang-cao\"\u003eGitHub Cookbook của Anthropic\u003c\/a\u003e. Bước tiếp theo: tìm hiểu \u003ca href=\"\/en\/collections\/ung-dung\"\u003eOCR với Claude\u003c\/a\u003e và \u003ca href=\"\/en\/collections\/ung-dung\"\u003ePhân tích biểu đồ và slides\u003c\/a\u003e.\u003c\/p\u003e\n\n\u003chr\u003e\n\u003ch3\u003eBài viết liên quan\u003c\/h3\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca href=\"\/en\/products\/calculator-tool-bai-h%E1%BB%8Dc-d%E1%BA%A7u-tien-v%E1%BB%81-tool-use-v%E1%BB%9Bi-claude\"\u003eCalculator Tool — Bài học đầu tiên về Tool Use với Claude\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/en\/products\/tool-evaluation-danh-gia-hi%E1%BB%87u-qu%E1%BA%A3-tools-trong-agent-systems\"\u003eTool Evaluation — Đánh giá hiệu quả tools trong agent systems\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/en\/products\/b%E1%BA%AFt-d%E1%BA%A7u-v%E1%BB%9Bi-claude-vision-g%E1%BB%ADi-hinh-%E1%BA%A3nh-qua-api\"\u003eBắt đầu với Claude Vision — Gửi hình ảnh qua API\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/en\/products\/building-effective-agents-v%E1%BB%9Bi-claude-h%C6%B0%E1%BB%9Bng-d%E1%BA%ABn-ki%E1%BA%BFn-truc\"\u003eBuilding Effective Agents với Claude — Hướng dẫn kiến trúc\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/en\/products\/claude-cho-data-data-visualization-nang-cao\"\u003eClaude cho Data: Data Visualization nâng cao\u003c\/a\u003e\u003c\/li\u003e\n\u003c\/ul\u003e","brand":"Minh Tuấn","offers":[{"title":"Default Title","offer_id":47721834447060,"sku":null,"price":0.0,"currency_code":"VND","in_stock":true}],"thumbnail_url":"\/\/cdn.shopify.com\/s\/files\/1\/0821\/0264\/9044\/files\/crop-tool-cho-claude-kh_-nang-zoom-vao-chi-ti_t-hinh-_nh_eda1cd93-da08-4edf-bffa-398427f87716.jpg?v=1774521741","url":"https:\/\/claude.vn\/en\/products\/crop-tool-cho-claude-kh%e1%ba%a3-nang-zoom-vao-chi-ti%e1%ba%bft-hinh-%e1%ba%a3nh","provider":"CLAUDE.VN","version":"1.0","type":"link"}