MCPcopy Create free account
hub / github.com/apple/ml-streambridge / read_image_patch

Function read_image_patch

streambridge/mm_utils.py:250–266  ·  view source on GitHub ↗
(patch_info)

Source from the content-addressed store, hash-verified

248
249
250def read_image_patch(patch_info):
251 if 'img_path' in patch_info.keys():
252 image = Image.open(patch_info['img_path']).convert('RGB')
253 else:
254 if 'image_encoing' in patch_info.keys():
255 patch_info['image_encoding'] = patch_info['image_encoing']
256 image_file_name = patch_info['patch']
257 start_bytes = int(patch_info['start_num'])
258 file_size = int(patch_info['size'])
259
260 with open(image_file_name, 'rb') as f:
261 f.seek(start_bytes)
262 if 'image_encoding' in patch_info.keys() and patch_info['image_encoding'] == 'base64':
263 image = Image.open(io.BytesIO(base64.b64decode(f.read(file_size).decode()))).convert("RGB")
264 else:
265 image = Image.open(io.BytesIO(f.read(file_size))).convert("RGB")
266 return image
267
268def tokenizer_image_token(prompt, tokenizer, image_token_index=IMAGE_TOKEN_INDEX, return_tensors=None):
269 prompt_chunks = [tokenizer(chunk).input_ids for chunk in prompt.split('<image>')]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected