-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
22 lines (20 loc) · 1008 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import base64
import os
from src.logger import get_logger
def encode_image_to_base64(image_path):
if not os.path.exists(image_path):
raise FileNotFoundError(f"The specified image file does not exist: {image_path}")
logger = get_logger('utils')
if not os.path.exists(image_path):
logger.error(f"The specified image file does not exist: {image_path}")
raise FileNotFoundError(f"The specified image file does not exist: {image_path}")
with open(image_path, 'rb') as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string
def validate_image_file(image_path):
logger = get_logger('utils')
if not os.path.isfile(image_path):
logger.error(f"Invalid image file path: {image_path}")
raise ValueError(f"Invalid image file path: {image_path}")
if not image_path.lower().endswith(('.png', '.jpg', '.jpeg')):
raise ValueError("Unsupported image format. Please use PNG or JPEG.")