"""Utility functions for image handling and processing.""" import base64 def ensure_bytes_format(image_data): """ Ensure image data is in the proper format for storage. Args: image_data: Image data that could be bytes, base64 string, or URL string Returns: Properly formatted data for database storage """ if image_data is None: return None # If it's already bytes, return as is if isinstance(image_data, bytes): return image_data # If it's a string, check if it's base64 encoded if isinstance(image_data, str): # Check if it's a data URL if image_data.startswith('data:image/'): try: # Extract base64 part and decode to bytes base64_part = image_data.split(',')[1] return base64.b64decode(base64_part) except Exception: # If decoding fails, store as string (URL) return image_data else: # Assume it's a URL, store as string return image_data # For any other type, return as is return image_data