16
16
17
17
SAMPLE = [
18
18
((113 , 183 ), (128 , 255 , 64 )),
19
+ ((290 , 160 ), (255 , 128 , 161 )),
19
20
((704 , 183 ), (255 , 242 , 179 )),
21
+ ((143 , 375 ), (255 , 191 , 82 )),
20
22
((452 , 405 ), (255 , 191 , 82 )),
21
- ((290 , 160 ), (255 , 128 , 161 )),
22
- ((372 , 484 ), (0 , 0 , 0 )),
23
+ ((585 , 386 ), (69 , 196 , 5 )),
23
24
]
24
25
25
26
@@ -28,22 +29,27 @@ class ImageError(Exception):
28
29
29
30
30
31
@cache
31
- def get_mask_image ():
32
+ def get_mask_image () -> Image :
32
33
with as_file (MASK_FILE_PATH ) as image_path :
33
34
return Image .open (image_path )
34
35
35
36
36
- def prepare_image (image : Image ):
37
+ def prepare_image (image : Image ) -> tuple [ Image , list [ int ]] :
37
38
if image .size != DIM :
38
39
logger .error (f"Wrong image dimensions: { image .size } , only { DIM } is valid" )
39
40
raise ImageError (f"Wrong image dimensions: { image .size } , only { DIM } is valid" )
40
41
palette = image .getpalette ()
41
42
palette = tuple (palette ) if palette else None
42
- for pixel , expected_color in SAMPLE :
43
+ good_panels : list [int ] = []
44
+ for panel_no , (pixel , expected_color ) in enumerate (SAMPLE , start = 1 ):
43
45
color = image .getpixel (pixel )
44
46
color = normalize_color (color , palette )
45
- if square_distance (color , expected_color ) > COLOR_THRESHOLD :
46
- logger .error (f"Invalid template: expected { expected_color } at { pixel } ; found { color } " )
47
- raise ImageError (f"Invalid template: expected { expected_color } at { pixel } ; found { color } " )
47
+ if square_distance (color , expected_color ) <= COLOR_THRESHOLD :
48
+ good_panels .append (panel_no )
49
+ else :
50
+ logger .warning (f"Invalid template: expected { expected_color } at { pixel } ; found { color } " )
51
+ if not good_panels :
52
+ logger .error ("Invalid template" )
53
+ raise ImageError (f"Invalid template" )
48
54
all_white = Image .new (mode = 'RGB' , size = DIM , color = (255 , 255 , 255 ))
49
- return Image .composite (image , all_white , get_mask_image ())
55
+ return Image .composite (image , all_white , get_mask_image ()), good_panels
0 commit comments