MCPcopy Create free account
hub / github.com/Open-Bee/DataStudio / load_and_encode_image

Function load_and_encode_image

datastudio/utils/database.py:489–517  ·  view source on GitHub ↗

Load image from disk and encode to bytes (no LMDB access).

(image_path: str)

Source from the content-addressed store, hash-verified

487 logger = sharded_manager.logger
488
489 def load_and_encode_image(image_path: str) -> Optional[Tuple[str, int, bytes]]:
490 """Load image from disk and encode to bytes (no LMDB access)."""
491 try:
492 shard_id = sharded_manager._get_shard_id(image_path)
493
494 # Process path
495 actual_path = image_path
496 if not os.path.isabs(image_path) and data_root:
497 actual_path = os.path.join(data_root, image_path)
498
499 # Read and process image
500 img = cv2.imread(actual_path)
501 if img is None:
502 return None
503 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
504 if resize_image:
505 img = apply_resize_image_cv2(img, resize_image_size)
506 img_pil = Image.fromarray(img)
507
508 # Convert to bytes
509 buffer = io.BytesIO()
510 img_pil.save(buffer, format="PNG")
511 img_bytes = buffer.getvalue()
512
513 return (image_path, shard_id, img_bytes)
514 except Exception as e:
515 if logger:
516 logger.warning(f"Error processing image {image_path}: {e}")
517 return None
518
519 processed_count = 0
520

Callers

nothing calls this directly

Calls 4

apply_resize_image_cv2Function · 0.90
_get_shard_idMethod · 0.80
warningMethod · 0.80
saveMethod · 0.45

Tested by

no test coverage detected