leonelhs commited on
Commit
bf20fc4
1 Parent(s): 1c09667

update utils

Browse files
Files changed (1) hide show
  1. utils.py +27 -0
utils.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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))
13
+ return r, g, b, 255
14
+
15
+
16
+ def apply_background_color(img: PILImage, color: Tuple[int, int, int, int]) -> PILImage:
17
+ r, g, b, a = color
18
+ colored_image = PIL.Image.new("RGBA", img.size, (r, g, b, a))
19
+ colored_image.paste(img, mask=img)
20
+ return colored_image
21
+
22
+
23
+ def make_flatten_background(img, mask):
24
+ img = PIL.Image.open(img)
25
+ mask = PIL.Image.open(mask)
26
+ color = get_background_dominant_color(img, mask)
27
+ return apply_background_color(img, color)