import os import logging from typing import Any, Dict from PIL import Image logger = logging.getLogger("MattCP") 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"} 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."} # Pillow parses PNG text chunks into the .info dictionary metadata = img.info if not metadata: return {"text": "No metadata found in this PNG."} # 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 Exception as e: return {"text": f"Error reading PNG metadata: {str(e)}"}