MCPcopy Create free account
hub / github.com/InternScience/SpectrumLab / prepare_images_for_prompt

Function prepare_images_for_prompt

spectrumlab/utils/image_utils.py:30–64  ·  view source on GitHub ↗
(
    image_paths: Union[str, List[str], None],
)

Source from the content-addressed store, hash-verified

28
29
30def prepare_images_for_prompt(
31 image_paths: Union[str, List[str], None],
32) -> List[Dict[str, Any]]:
33 if not image_paths:
34 return []
35
36 # Ensure it's a list format
37 if isinstance(image_paths, str):
38 image_paths = [image_paths]
39
40 image_data = []
41 for image_path in image_paths:
42 if not image_path or not image_path.strip():
43 continue
44
45 path = Path(image_path)
46 if not path.exists():
47 print(f"⚠️ Warning: Image file not found: {image_path}")
48 continue
49
50 try:
51 base64_image = encode_image_to_base64(image_path)
52 mime_type = get_image_mime_type(image_path)
53
54 image_info = {
55 "type": "image_url",
56 "image_url": {"url": f"data:{mime_type};base64,{base64_image}"},
57 }
58 image_data.append(image_info)
59
60 except Exception as e:
61 print(f"⚠️ Warning: Failed to process image {image_path}: {e}")
62 continue
63
64 return image_data
65
66
67def normalize_image_paths(image_paths_field: Any) -> Optional[List[str]]:

Callers 3

_build_promptMethod · 0.90
_build_score_promptMethod · 0.90
_build_promptMethod · 0.90

Calls 2

encode_image_to_base64Function · 0.85
get_image_mime_typeFunction · 0.85

Tested by

no test coverage detected