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

Method encode_image_directly

datastudio/models/base.py:114–137  ·  view source on GitHub ↗

Encode image directly without caching. Args: img_item: PIL Image object or other image data. Returns: Base64 encoded string or original data.

(self, img_item)

Source from the content-addressed store, hash-verified

112 raise NotImplementedError("Subclasses must implement generate_inner method")
113
114 def encode_image_directly(self, img_item):
115 """Encode image directly without caching.
116
117 Args:
118 img_item: PIL Image object or other image data.
119
120 Returns:
121 Base64 encoded string or original data.
122 """
123 if isinstance(img_item, Image.Image):
124 # Encode directly without caching
125 try:
126 return encode_image_to_base64(img_item)
127 except Exception as e:
128 if self.logger:
129 self.logger.warning(f"Image encoding failed: {str(e)}")
130 raise
131 elif isinstance(img_item, str) and img_item.startswith("data:image"):
132 # Already in base64 format, return directly
133 return img_item
134 else:
135 # Other cases (e.g., file path), open and encode
136 img = Image.open(img_item)
137 return self.encode_image_directly(img)
138
139 def check_content(self, msgs):
140 """Check input content type.

Callers 5

prepare_inputsMethod · 0.95
encode_taskMethod · 0.95

Calls 2

encode_image_to_base64Function · 0.90
warningMethod · 0.80