extract_images
Extract images from Word documents by providing a file path, optionally saving to a directory or returning base64 data.
Instructions
Extract all images from a Word document.
Args: filepath: Path to the document output_dir: Directory to save extracted images (optional) return_base64: If True, return images as base64 encoded strings
Returns: Dictionary with extracted images info and optionally base64 data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| output_dir | No | ||
| return_base64 | No |
Implementation Reference
- src/docx_mcp/server.py:1114-1182 (handler)The 'extract_images' tool implementation, including the @app.tool() decorator and the core logic for extracting and optionally saving or base64-encoding images from a Word document.
@app.tool() def extract_images( filepath: str, output_dir: Optional[str] = None, return_base64: bool = False, ) -> dict[str, Any]: """ Extract all images from a Word document. Args: filepath: Path to the document output_dir: Directory to save extracted images (optional) return_base64: If True, return images as base64 encoded strings Returns: Dictionary with extracted images info and optionally base64 data """ import base64 logger.info("Extracting images", extra={"tool": "extract_images", "filepath": filepath}) try: doc = safe_open_document(filepath) extracted = [] image_index = 0 for rel in doc.part.rels.values(): if "image" in rel.target_ref: image_part = rel.target_part image_data = image_part.blob filename = Path(image_part.partname).name content_type = image_part.content_type image_info = { "index": image_index, "filename": filename, "content_type": content_type, "size_bytes": len(image_data), } # Save to output directory if specified if output_dir: out_path = normalize_path(output_dir) out_path.mkdir(parents=True, exist_ok=True) image_file = out_path / filename image_file.write_bytes(image_data) image_info["saved_path"] = str(image_file) # Return base64 if requested if return_base64: image_info["base64"] = base64.b64encode(image_data).decode("utf-8") extracted.append(image_info) image_index += 1 logger.info(f"Extracted {len(extracted)} images", extra={"filepath": filepath}) return { "status": "success", "filepath": filepath, "images": extracted, "count": len(extracted), } except DocxMcpError as e: logger.warning(e.message, extra={"tool": "extract_images", "error_code": e.error_code}) return {"status": "error", "error": e.message, "error_code": e.error_code} except Exception as e: logger.error(f"Unexpected error extracting images: {str(e)}") return {"status": "error", "error": str(e)}