-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_palette.py
More file actions
46 lines (40 loc) · 1.32 KB
/
apply_palette.py
File metadata and controls
46 lines (40 loc) · 1.32 KB
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
# failes experiment
from PIL import Image
import numpy as np
import sys
import pathlib
from . import palettes
@np.vectorize
def _mul(a, b):
return np.uint16(a)*b//255
color_warned = False
def load_base_image(filename):
global color_warned
filename = pathlib.Path(filename)
"Returns a tuple of two numpy arrays representing the gray and alpha channels."
imgp = Image.open(filename)
alpha = None # np.uint8(255)
# scalar will be broadcast when used in final calculation
grays = None
if imgp.mode in ('RGBA', 'RGB', 'P'):
print(f'Warning: converting color image {filename.stem} to grayscale.')
if not color_warned:
color_warned = True
print(f'Warning: The meaning of color pixels might change in the future.')
imgp = imgp.convert('LA')
imga = np.array(imgp)
grays = imga[:,:,0]
alpha = imga[:,:,1]
return grays, alpha
def apply_palette(srcvalue, srcalpha, palette):
result = palette[srcvalue]
if srcalpha is not None:
result[...,3] = _mul(result[...,3], srcalpha)
return Image.fromarray(result, 'RGBA')
def main(imgfn, palfn, dstfn):
grays, alpha = load_base_image(imgfn)
pal = palettes.load_palette(palfn)
dstimg = apply_palette(grays, alpha, pal)
dstimg.save(dstfn)
if __name__ == '__main__':
main(*sys.argv[1:])