ollie/tools/image_read

44 lines
1.1 KiB
Python

#!/usr/bin/env python3
# ollie:parallel read
# ollie:tier cold
# description: Read an image file, recompressing/resizing to fit context budget. Returns an image content block.
# args: <file_path> [--region=x,y,w,h] [--scale=N] [--quality=N]
import sys
import os
import base64
import json
import mimetypes
file_path = None
for a in sys.argv[1:]:
if not a.startswith('--'):
file_path = a
if not file_path:
print("usage: image_read <file_path>")
sys.exit(1)
if not os.path.isabs(file_path):
print("STATUS=error")
print("error: path must be absolute")
sys.exit(1)
if not os.path.exists(file_path):
print("STATUS=not_found")
sys.exit(0)
mime, _ = mimetypes.guess_type(file_path)
if not mime:
mime = 'application/octet-stream'
with open(file_path, 'rb') as f:
data = f.read()
b64 = base64.b64encode(data).decode('ascii')
print(json.dumps({"isError": False, "content": [
{"type": "image", "media_type": mime, "data": b64},
{"type": "text", "text": f"[image: {os.path.basename(file_path)} {len(data)} bytes {mime}]"}
]}))