jean
first commit
1926f81
Raw
History Blame Contribute Delete
610 Bytes
import base64
import io
from PIL import Image
def image_to_base64_data_uri(file_path, size=(224, 224)):
# Open the image
with Image.open(file_path) as img:
# Convert to RGB (in case it's RGBA or grayscale)
img = img.convert("RGB")
# Resize the image
img = img.resize(size)
# Save resized image to a BytesIO buffer
buffer = io.BytesIO()
img.save(buffer, format="JPEG")
buffer.seek(0)
# Encode to base64
base64_data = base64.b64encode(buffer.read()).decode('utf-8')
return f"data:image/jpeg;base64,{base64_data}"