haydpw's picture
change calculation
9471dba
raw
history blame
520 Bytes
from PIL import Image
import io
import base64
import numpy as np
def image_to_base64(image: Image.Image) -> str:
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
return base64.b64encode(buffered.getvalue()).decode("utf-8")
def calculate_mask_area(mask: Image.Image, background=False) -> int:
mask_array = np.array(mask)
if background:
non_zero_pixels = np.count_nonzero(mask_array == 0)
else:
non_zero_pixels = np.count_nonzero(mask_array)
return non_zero_pixels