Delete tools/file/analyze_image.py
Browse files- tools/file/analyze_image.py +0 -73
tools/file/analyze_image.py
DELETED
|
@@ -1,73 +0,0 @@
|
|
| 1 |
-
from langchain_core.tools import tool
|
| 2 |
-
from typing import Dict, Any
|
| 3 |
-
import base64
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io
|
| 6 |
-
import os
|
| 7 |
-
import uuid
|
| 8 |
-
import numpy as np
|
| 9 |
-
|
| 10 |
-
# Helper functions for image processing
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def decode_image(base64_string: str) -> Image.Image:
|
| 14 |
-
"""Convert a base64 string to a PIL Image."""
|
| 15 |
-
image_data = base64.b64decode(base64_string)
|
| 16 |
-
return Image.open(io.BytesIO(image_data))
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def save_image(image: Image.Image, directory: str = "image_outputs") -> str:
|
| 20 |
-
"""Save a PIL Image to disk and return the path."""
|
| 21 |
-
os.makedirs(directory, exist_ok=True)
|
| 22 |
-
image_id = str(uuid.uuid4())
|
| 23 |
-
image_path = os.path.join(directory, f"{image_id}.png")
|
| 24 |
-
image.save(image_path)
|
| 25 |
-
return image_path
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def encode_image(image_path: str) -> str:
|
| 29 |
-
"""Convert an image file to base64 string."""
|
| 30 |
-
with open(image_path, "rb") as image_file:
|
| 31 |
-
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
@tool
|
| 35 |
-
def analyze_image(image_base64: str) -> Dict[str, Any]:
|
| 36 |
-
"""
|
| 37 |
-
Analyze basic properties of an image (size, mode, color analysis, thumbnail preview).
|
| 38 |
-
Args:
|
| 39 |
-
image_base64 (str): Base64 encoded image string
|
| 40 |
-
Returns:
|
| 41 |
-
Dictionary with analysis result
|
| 42 |
-
"""
|
| 43 |
-
try:
|
| 44 |
-
img = decode_image(image_base64)
|
| 45 |
-
width, height = img.size
|
| 46 |
-
mode = img.mode
|
| 47 |
-
|
| 48 |
-
if mode in ("RGB", "RGBA"):
|
| 49 |
-
arr = np.array(img)
|
| 50 |
-
avg_colors = arr.mean(axis=(0, 1))
|
| 51 |
-
dominant = ["Red", "Green", "Blue"][np.argmax(avg_colors[:3])]
|
| 52 |
-
brightness = avg_colors.mean()
|
| 53 |
-
color_analysis = {
|
| 54 |
-
"average_rgb": avg_colors.tolist(),
|
| 55 |
-
"brightness": brightness,
|
| 56 |
-
"dominant_color": dominant,
|
| 57 |
-
}
|
| 58 |
-
else:
|
| 59 |
-
color_analysis = {"note": f"No color analysis for mode {mode}"}
|
| 60 |
-
|
| 61 |
-
thumbnail = img.copy()
|
| 62 |
-
thumbnail.thumbnail((100, 100))
|
| 63 |
-
thumb_path = save_image(thumbnail, "thumbnails")
|
| 64 |
-
thumbnail_base64 = encode_image(thumb_path)
|
| 65 |
-
|
| 66 |
-
return {
|
| 67 |
-
"dimensions": (width, height),
|
| 68 |
-
"mode": mode,
|
| 69 |
-
"color_analysis": color_analysis,
|
| 70 |
-
"thumbnail": thumbnail_base64,
|
| 71 |
-
}
|
| 72 |
-
except Exception as e:
|
| 73 |
-
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|