File size: 7,115 Bytes
aecf8ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | """
Image Processor Utilities.
This module handles image processing for structure diagrams and scanned documents.
Design Note:
Image processing here is limited to:
- Basic image validation
- Path management
- Potential OCR preparation (TODO)
Actual chemical structure recognition would require external services
(e.g., OSRA, ChemDraw API) and is out of scope for this implementation.
"""
from typing import Optional, Tuple
from pathlib import Path
import base64
class ImageProcessor:
"""
Image processor for structure diagrams and scanned documents.
Primary responsibilities:
- Validate image files
- Prepare images for LLM vision APIs (if supported)
- Encode images for embedding
"""
SUPPORTED_FORMATS = {'.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp'}
def __init__(self):
"""Initialize the image processor."""
self._pil_available = self._check_pil()
def _check_pil(self) -> bool:
"""Check if PIL/Pillow is available."""
try:
from PIL import Image
return True
except ImportError:
return False
def validate_image(self, file_path: str) -> bool:
"""
Validate that a file is a readable image.
Args:
file_path: Path to the image file
Returns:
True if valid image, False otherwise
"""
path = Path(file_path)
# Check extension
if path.suffix.lower() not in self.SUPPORTED_FORMATS:
return False
# Check file exists
if not path.exists():
return False
# Try to open with PIL if available
if self._pil_available:
try:
from PIL import Image
with Image.open(file_path) as img:
img.verify()
return True
except Exception:
return False
return True
def get_image_info(self, file_path: str) -> Optional[dict]:
"""
Get basic information about an image.
Args:
file_path: Path to the image file
Returns:
Dictionary with image info, or None if failed
"""
if not self._pil_available:
return {"path": file_path, "status": "PIL not available"}
try:
from PIL import Image
with Image.open(file_path) as img:
return {
"path": file_path,
"format": img.format,
"mode": img.mode,
"size": img.size, # (width, height)
"width": img.size[0],
"height": img.size[1],
}
except Exception as e:
return {"path": file_path, "error": str(e)}
def encode_base64(self, file_path: str) -> Optional[str]:
"""
Encode an image as base64 string.
Useful for embedding in HTML or sending to vision APIs.
Args:
file_path: Path to the image file
Returns:
Base64 encoded string, or None if failed
"""
try:
with open(file_path, "rb") as f:
image_data = f.read()
return base64.b64encode(image_data).decode('utf-8')
except Exception as e:
print(f"Error encoding image: {e}")
return None
def get_data_uri(self, file_path: str) -> Optional[str]:
"""
Get a data URI for embedding an image directly in HTML.
Args:
file_path: Path to the image file
Returns:
Data URI string, or None if failed
"""
path = Path(file_path)
suffix = path.suffix.lower()
# Map extension to MIME type
mime_types = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.bmp': 'image/bmp',
}
mime_type = mime_types.get(suffix, 'image/png')
base64_data = self.encode_base64(file_path)
if base64_data:
return f"data:{mime_type};base64,{base64_data}"
return None
def resize_for_report(
self,
file_path: str,
max_width: int = 400,
max_height: int = 300
) -> Optional[str]:
"""
Resize an image for report embedding.
Creates a temporary resized copy suitable for report generation.
Args:
file_path: Path to the original image
max_width: Maximum width in pixels
max_height: Maximum height in pixels
Returns:
Path to resized image, or original path if resizing fails
"""
if not self._pil_available:
return file_path
try:
from PIL import Image
import tempfile
with Image.open(file_path) as img:
# Calculate new size maintaining aspect ratio
img.thumbnail((max_width, max_height), Image.Resampling.LANCZOS)
# Save to temp file
suffix = Path(file_path).suffix
with tempfile.NamedTemporaryFile(
suffix=suffix,
delete=False
) as tmp:
img.save(tmp.name)
return tmp.name
except Exception as e:
print(f"Error resizing image: {e}")
return file_path
def prepare_for_llm(self, file_path: str) -> Optional[dict]:
"""
Prepare an image for LLM vision API submission.
Returns a dictionary suitable for vision model APIs.
Args:
file_path: Path to the image file
Returns:
Dictionary with image data for API submission
"""
if not self.validate_image(file_path):
return None
base64_data = self.encode_base64(file_path)
if not base64_data:
return None
path = Path(file_path)
mime_types = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
}
mime_type = mime_types.get(path.suffix.lower(), 'image/png')
return {
"type": "image",
"source": {
"type": "base64",
"media_type": mime_type,
"data": base64_data,
}
}
|