File size: 1,877 Bytes
d0c641b
08fa61a
 
 
 
da8cc4f
08fa61a
 
da8cc4f
08fa61a
 
9471dba
08fa61a
d461f5e
da8cc4f
 
9d7fc49
f0aee21
da8cc4f
 
 
 
9d7fc49
f0aee21
 
 
 
 
 
9d7fc49
 
 
f0aee21
 
 
da8cc4f
 
f0aee21
da8cc4f
f0aee21
d0c641b
 
 
 
 
 
 
 
 
36d80a3
 
d0c641b
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from PIL import Image, ImageChops, ImageEnhance
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, fill_color:tuple = (255, 255, 255)) -> Image.Image:


    data = np.array(input_image)
    # Split the image into its component channels

    # Create a mask where all pixels that are black (0) will have 0 alpha
    black_areas = data == 0
    
    rgba_image = Image.new('RGBA', input_image.size)
    rgba_data = np.array(rgba_image)

    # Copy the grayscale data to all RGB channels
    rgba_data[..., 0] = fill_color[0]
    rgba_data[..., 1] = fill_color[1]
    rgba_data[..., 2] = fill_color[2]

    # Set alpha channel to 255 (fully opaque) for all pixels
    rgba_data[..., 3] = 255

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

    return Image.fromarray(rgba_data)

def combine_images(original_image: Image.Image, masks: list) -> Image.Image:
    combined = original_image.copy()
    for mask in masks:
        if mask['label'] == 'background':
            continue
        mask_image = Image.open(io.BytesIO(base64.b64decode(mask['mask'])))
        mask_image = mask_image.convert("L")  # Convert mask to grayscale
        mask_image = ImageEnhance.Brightness(mask_image).enhance(0.5)  # Adjust the brightness to make it more visible
        color_mask = Image.new("RGBA", original_image.size, (255, 0, 0, 128))  # Red color with transparency
        combined.paste(color_mask, (0, 0), mask_image)
    return combined