File size: 1,054 Bytes
c741672 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import io, base64
import json
def image_to_base64(image):
"""Convert PIL Image to base64 encoded string"""
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='JPEG')
return base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
def load_questions(filepath="data/all_questions.json"):
"""Loads questions from a JSON file.
Args:
filepath (str): The path to the JSON file. Defaults to "data/all_questions.json".
Returns:
list: A list of question records. Returns an empty list if the file is not found or if an error occurs during loading.
"""
try:
with open(filepath, 'r') as f:
data = json.load(f)
return data
except FileNotFoundError:
print(f"Error: File not found at {filepath}")
return []
except json.JSONDecodeError:
print(f"Error: Could not decode JSON from {filepath}. The file may be corrupted.")
return []
except Exception as e:
print(f"An unexpected error occurred: {e}")
return []
|