forked from kylemcdonald/python-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_conversion.py
More file actions
29 lines (27 loc) · 807 Bytes
/
color_conversion.py
File metadata and controls
29 lines (27 loc) · 807 Bytes
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
import numpy as np
def to_single_rgb(img):
img = np.asarray(img)
if len(img.shape) == 4: # take first frame from animations
return img[0,:,:,:]
if len(img.shape) == 2: # convert gray to rgb
img = img[:,:,np.newaxis]
return np.repeat(img, 3, 2) # might np.tile(img, [1,1,3]) be faster?
if img.shape[-1] == 4: # drop alpha
return img[:,:,:3]
else:
return img
def to_single_gray(img):
img = np.asarray(img)
if len(img.shape) == 2:
return img[:,:,np.newaxis]
elif img.shape[2] == 3:
return img.mean(axis=2)
else:
return img
def rb_swap(img):
if len(img.shape) < 3:
return img
if img.shape[2] == 3:
return img[...,(2,1,0)]
if img.shape[2] == 4:
return img[...,(2,1,0,3)]