This commit is contained in:
2026-07-22 22:53:15 -07:00
parent d87fcb85be
commit 48132aa21c
6 changed files with 149 additions and 135 deletions
+11 -6
View File
@@ -1,7 +1,8 @@
import os
import logging
from pathlib import Path
from typing import Any, Dict
from PIL import Image
from tools.utils import ToolError
logger = logging.getLogger("MattCP")
@@ -10,15 +11,17 @@ async def handle(args: Dict[str, Any]):
Extracts text-based metadata chunks (tEXt, zTXt, iTXt) from a PNG file.
These often contain AI generation prompts and parameters.
"""
path = args.get("path")
if not path:
return {"text": "Error: Missing path argument"}
path_str = args.get("path")
if not path_str:
raise ToolError("Missing path argument")
path = Path(path_str)
try:
with Image.open(path) as img:
# Metadata extraction is specific to PNG format in this implementation
if img.format != 'PNG':
return {"text": "Error: File is not a PNG image."}
raise ToolError("File is not a PNG image.")
# Pillow parses PNG text chunks into the .info dictionary
metadata = img.info
@@ -28,5 +31,7 @@ async def handle(args: Dict[str, Any]):
# Format as a simple key: value list
output = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
return {"text": f"PNG Metadata:\n{output}"}
except ToolError:
raise
except Exception as e:
return {"text": f"Error reading PNG metadata: {str(e)}"}
raise ToolError(f"Error reading PNG metadata: {str(e)}")