Update utils.py
Browse files
utils.py
CHANGED
@@ -2,11 +2,12 @@ from typing import Tuple
|
|
2 |
|
3 |
import PIL
|
4 |
from PIL.Image import Image as PILImage
|
|
|
5 |
|
6 |
|
7 |
def get_background_dominant_color(img: PILImage, mask: PILImage) -> tuple:
|
8 |
negative_img = img.copy()
|
9 |
-
negative_mask = PIL.ImageOps.invert(mask)
|
10 |
negative_img.putalpha(negative_mask)
|
11 |
negative_img = negative_img.resize((1, 1))
|
12 |
r, g, b, a = negative_img.getpixel((0, 0))
|
@@ -20,8 +21,10 @@ def apply_background_color(img: PILImage, color: Tuple[int, int, int, int]) -> P
|
|
20 |
return colored_image
|
21 |
|
22 |
|
23 |
-
def make_flatten_background(
|
24 |
-
|
|
|
25 |
mask = PIL.Image.open(mask)
|
26 |
-
color = get_background_dominant_color(
|
27 |
-
return apply_background_color(
|
|
|
|
2 |
|
3 |
import PIL
|
4 |
from PIL.Image import Image as PILImage
|
5 |
+
from PIL import ImageOps
|
6 |
|
7 |
|
8 |
def get_background_dominant_color(img: PILImage, mask: PILImage) -> tuple:
|
9 |
negative_img = img.copy()
|
10 |
+
negative_mask = PIL.ImageOps.invert(mask).convert("L")
|
11 |
negative_img.putalpha(negative_mask)
|
12 |
negative_img = negative_img.resize((1, 1))
|
13 |
r, g, b, a = negative_img.getpixel((0, 0))
|
|
|
21 |
return colored_image
|
22 |
|
23 |
|
24 |
+
def make_flatten_background(img_original, img_cutted, mask):
|
25 |
+
img_original = PIL.Image.open(img_original)
|
26 |
+
img_cutted = PIL.Image.open(img_cutted)
|
27 |
mask = PIL.Image.open(mask)
|
28 |
+
color = get_background_dominant_color(img_original, mask)
|
29 |
+
return apply_background_color(img_cutted, color)
|
30 |
+
|