MCPcopy Create free account
hub / github.com/Hongcheng-Gao/SpatialWorld / encode_image

Function encode_image

scripts/utils/api_utils.py:285–308  ·  view source on GitHub ↗

base64 Args: image_input: PIL.Image Returns: base64 Raises: ValueError:

(image_input: Union[str, Image.Image])

Source from the content-addressed store, hash-verified

283
284
285def encode_image(image_input: Union[str, Image.Image]) -> str:
286 """
287 base64
288
289 Args:
290 image_input: PIL.Image
291
292 Returns:
293 base64
294
295 Raises:
296 ValueError:
297 """
298 if isinstance(image_input, str):
299 #
300 with open(image_input, "rb") as image_file:
301 return base64.b64encode(image_file.read()).decode("utf-8")
302 elif isinstance(image_input, Image.Image):
303 # PIL.Image
304 buffer = io.BytesIO()
305 image_input.save(buffer, format='PNG')
306 return base64.b64encode(buffer.getvalue()).decode("utf-8")
307 else:
308 raise ValueError("image_input must be a file path string or PIL.Image object")
309
310
311def tools_to_text_description(tools: List[Dict[str, Any]]) -> str:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected