Files
wydt/utils.py

24 lines
594 B
Python

import base64
import json
from jsonschema import validate, ValidationError
def img2base64(image_path: str) -> str:
"""
Converts image to base64 encoded string
"""
with open(image_path, "rb") as f:
base64_encoded = base64.b64encode(f.read()).decode("utf-8")
return f"data:image/png;base64,{base64_encoded}"
def is_valid_json_schema(json_string: str, schema: dict) -> bool:
try:
data = json.loads(json_string)
validate(instance=data, schema=schema)
return True
except (json.JSONDecodeError, ValidationError):
return False