File size: 934 Bytes
08fa61a
 
 
 
 
da8cc4f
08fa61a
 
da8cc4f
08fa61a
 
9471dba
08fa61a
d461f5e
da8cc4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from PIL import Image
import io
import base64
import numpy as np

def image_to_base64(image: Image.Image,format="JPEG") -> str:
    buffered = io.BytesIO()

    image.save(buffered, format=format)
    return base64.b64encode(buffered.getvalue()).decode("utf-8")

def calculate_mask_area(mask: Image.Image, background=False) -> int:
    mask_array = np.array(mask)
    non_zero_pixels = np.count_nonzero(mask_array)
    return non_zero_pixels

def process_image(input_image: Image.Image) -> Image.Image:
    if input_image.mode != 'RGBA':
        input_image = input_image.convert('RGBA')

    data = np.array(input_image)
    # Split the image into its component channels
    r, g, b, a = data.T

    # Create a mask where all pixels that are black (0,0,0) will have 0 alpha
    black_areas = (r == 0) & (g == 0) & (b == 0)

    # Apply the mask to the alpha channel
    data[..., 3][black_areas] = 0

    return Image.fromarray(data)