-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
29 lines (21 loc) · 881 Bytes
/
tools.py
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
from PIL import ImageTk, Image
from config import *
def flattenAlpha(img):
global SCALE
[img_w, img_h] = img.size
img = img.resize((int(img_w * SCALE), int(img_h * SCALE)), Image.ANTIALIAS)
alpha = img.split()[-1] # Pull off the alpha layer
ab = alpha.tobytes() # Original 8-bit alpha
checked = [] # Create a new array to store the cleaned up alpha layer bytes
# Walk through all pixels and set them either to 0 for transparent or 255 for opaque fancy pants
transparent = 50 # change to suit your tolerance for what is and is not transparent
p = 0
for pixel in range(0, len(ab)):
if ab[pixel] < transparent:
checked.append(0) # Transparent
else:
checked.append(255) # Opaque
p += 1
mask = Image.frombytes('L', img.size, bytes(checked))
img.putalpha(mask)
return img