File size: 409 Bytes
08fa61a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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) -> int:
    mask_array = np.array(mask)
    non_zero_pixels = np.count_nonzero(mask_array)
    return non_zero_pixels