File size: 359 Bytes
6ad0b38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from PIL import Image
import numpy as np


def replace_background(image: Image.Image, new_background_color=(0, 255, 255)):
    image_np = np.array(image)

    white_threshold = 255 * 3
    white_pixels = np.sum(image_np, axis=-1) == white_threshold

    image_np[white_pixels] = new_background_color

    result = Image.fromarray(image_np)

    return result