-
Notifications
You must be signed in to change notification settings - Fork 2
/
valuechange.py
47 lines (31 loc) · 1.18 KB
/
valuechange.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Changes the value of the original image to make it more accessible for colorblind viewers.
# from PIL import Image
import cv2
import numpy as np
from PIL import Image
def cb_compatible(img, val_shift):
im_rgb = np.array(img)
# Converts to BGR scale for CV2 compatibility
im_bgr = im_rgb[:, :, ::-1].copy()
# Converts to HSV for hue modification
im_hsv = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2HSV)
if val_shift == "b":
# Shifts hue to blue
shift_n = 180 // 2
im_hsv[:, :, 0] = (im_hsv[:, :, 0].astype(int) + shift_n) % 181
if val_shift == "g":
# Shifts hue to green
shift_n = 90 // 2
im_hsv[:, :, 0] = (im_hsv[:, :, 0].astype(int) + shift_n) % 181
if val_shift == "r":
# Shifts hue to red
shift_n = 45 // 2
im_hsv[:, :, 0] = (im_hsv[:, :, 0].astype(int) + shift_n) % 181
# Converts file back to RGB
im_rgb_mod = cv2.cvtColor(im_hsv, cv2.COLOR_HSV2RGB)
im_rgb_mod = Image.fromarray(im_rgb_mod, 'RGB')
return im_rgb_mod
# img = (Image.open("ishihara.jpg"))
# val_shift = "g"
# new_img = cb_compatible(img, val_shift)
# new_img.show()