-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_image.py
71 lines (49 loc) · 1.78 KB
/
my_image.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from PIL import Image, ImageFilter
import numpy as np
import os
import logging
logger = logging.getLogger(__name__)
class MyImage(object):
def __init__(self, template_path, cond, colours_dict):
'''template_path is b&w template'''
self.template_path = os.path.abspath(template_path)
self.cond = cond
_, name = os.path.split(self.template_path)
self.name, ext = os.path.splitext(name)
if cond == 'magno':
self.fg = colours_dict['fg_grey']
self.bg = colours_dict['bg_grey']
elif cond == 'parvo':
self.fg = colours_dict['fg_col']
self.bg = colours_dict['bg_col']
elif cond == 'unbiased':
self.fg = [0,0,0]
self.bg = colours_dict['bg_grey']
self.global_letter = self.name[0].lower()
self.local_letter = self.name[1].lower()
def get_response_type(self, letter):
letter = letter.lower()
if letter == self.global_letter:
return 'global'
elif letter == self.local_letter:
return 'local'
def has_letter(self, letter):
if letter.lower() in [self.global_letter, self.local_letter]:
return True
else:
return False
def is_congruent(self):
if self.global_letter == self.local_letter:
return True
else:
return False
def make_image(self, blur = 1):
arr = np.array(Image.open(self.template_path))
fg_mask = arr[:, :, 0] == 0
arr[fg_mask] = self.fg
arr[~fg_mask] = self.bg
img = Image.fromarray(arr)
if blur:
img = img.filter(ImageFilter.GaussianBlur(radius = blur))
logger.info('created {} {} image'.format(self.cond, self.name))
return img