Spaces:
Build error
Build error
Romain Lembo
commited on
Commit
·
35d86b1
1
Parent(s):
f9c2e6d
Add image processing tools: get_image_properties, decode_image, encode_image, and save_image
Browse files- tools/get_image_properties.py +48 -0
- utils/decode_image.py +8 -0
- utils/encode_image.py +7 -0
- utils/save_image.py +11 -0
tools/get_image_properties.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy
|
| 2 |
+
|
| 3 |
+
from langchain_core.tools import tool
|
| 4 |
+
from typing import Any, Dict
|
| 5 |
+
from utils.decode_image import decode_image
|
| 6 |
+
from utils.encode_image import encode_image
|
| 7 |
+
from utils.save_image import save_image
|
| 8 |
+
|
| 9 |
+
@tool
|
| 10 |
+
def get_image_properties(image_base64: str) -> Dict[str, Any]:
|
| 11 |
+
"""
|
| 12 |
+
Get basic properties of an image (mode, size, color analysis, thumbnail preview).
|
| 13 |
+
Args:
|
| 14 |
+
image_base64 (str): Base64 encoded image string
|
| 15 |
+
Returns:
|
| 16 |
+
Dictionary with analysis result
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
img = decode_image(image_base64)
|
| 20 |
+
width, height = img.size
|
| 21 |
+
mode = img.mode
|
| 22 |
+
|
| 23 |
+
if mode in ("RGB", "RGBA"):
|
| 24 |
+
arr = numpy.array(img)
|
| 25 |
+
avg_colors = arr.mean(axis=(0, 1))
|
| 26 |
+
dominant = ["Red", "Green", "Blue"][numpy.argmax(avg_colors[:3])]
|
| 27 |
+
brightness = avg_colors.mean()
|
| 28 |
+
color_analysis = {
|
| 29 |
+
"average_rgb": avg_colors.tolist(),
|
| 30 |
+
"brightness": brightness,
|
| 31 |
+
"dominant_color": dominant,
|
| 32 |
+
}
|
| 33 |
+
else:
|
| 34 |
+
color_analysis = {"note": f"No color analysis for mode {mode}"}
|
| 35 |
+
|
| 36 |
+
thumbnail = img.copy()
|
| 37 |
+
thumbnail.thumbnail((100, 100))
|
| 38 |
+
thumb_path = save_image(thumbnail, "thumbnails")
|
| 39 |
+
thumbnail_base64 = encode_image(thumb_path)
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
"mode": mode,
|
| 43 |
+
"dimensions": (width, height),
|
| 44 |
+
"color_analysis": color_analysis,
|
| 45 |
+
"thumbnail": thumbnail_base64,
|
| 46 |
+
}
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return {"error": str(e)}
|
utils/decode_image.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import base64
|
| 3 |
+
import io
|
| 4 |
+
|
| 5 |
+
def decode_image(base64_string: str) -> Image.Image:
|
| 6 |
+
"""Convert a base64 string to a PIL Image."""
|
| 7 |
+
image_data = base64.b64decode(base64_string)
|
| 8 |
+
return Image.open(io.BytesIO(image_data))
|
utils/encode_image.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
|
| 3 |
+
# Helper functions for image processing
|
| 4 |
+
def encode_image(image_path: str) -> str:
|
| 5 |
+
"""Convert an image file to base64 string."""
|
| 6 |
+
with open(image_path, "rb") as image_file:
|
| 7 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
utils/save_image.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
def save_image(image: Image.Image, directory: str = "image_outputs") -> str:
|
| 6 |
+
"""Save a PIL Image to disk and return the path."""
|
| 7 |
+
os.makedirs(directory, exist_ok=True)
|
| 8 |
+
image_id = str(uuid.uuid4())
|
| 9 |
+
image_path = os.path.join(directory, f"{image_id}.png")
|
| 10 |
+
image.save(image_path)
|
| 11 |
+
return image_path
|