@@ -6,18 +6,21 @@ def reject_outliers(data, m = 6.):
6
6
if not isinstance (data , np .ndarray ):
7
7
data = np .array (data )
8
8
d = np .abs (data - np .median (data ))
9
- #print(d)
10
9
mdev = np .median (d )
11
10
s = d / (mdev if mdev else 1. )
12
-
13
- #print(s)
14
11
output = data [s < m ].tolist ()
12
+
13
+ # sometimes numpy tolist() returns a nested list
15
14
if type (output [0 ]) == list :
16
15
return output [0 ]
16
+
17
17
return output
18
18
19
+ def clamp (x ):
20
+ return max (0 , min (x , 255 ))
21
+
19
22
def rgb_to_hex (r , g , b ):
20
- return ( '#{:X}{:X}{:X}' ) .format (r , g , b )
23
+ return "#{0:02x}{1:02x}{2:02x}" .format (clamp ( r ), clamp ( g ), clamp ( b ) )
21
24
22
25
def average_pixels (img , pixels_coords , quiet ):
23
26
r , g , b = img .convert ('RGB' ).split ()
@@ -34,6 +37,7 @@ def average_pixels(img, pixels_coords, quiet):
34
37
r_list .append (r .getpixel ((x , y )))
35
38
g_list .append (g .getpixel ((x , y )))
36
39
b_list .append (b .getpixel ((x , y )))
40
+
37
41
if not quiet :
38
42
img_size = img .width * img .height
39
43
print (img .width , img .height )
@@ -90,6 +94,7 @@ def downscale_image(image_path, quiet):
90
94
img = Image .open (image_path ) # open source image as PIL/Pillow object
91
95
except IOError :
92
96
print ('%s could not be opened' % image_path )
97
+ return None
93
98
width , height = img .size
94
99
95
100
aspect_ratio = height / width
@@ -118,7 +123,11 @@ def decode(image_path, show_preview = False, quiet = True):
118
123
if show_preview :
119
124
img_size = img .size
120
125
preview_size = (round (img .width * 1.5 ), round (img .height * 1.5 ))
121
- preview_img = Image .new ("RGB" , preview_size , average_color )
126
+
127
+ # for some reason PIL doesn't play nicely with hex inputs here
128
+ # manually convert back to RGB here since occasionally it won't accept certain values
129
+ preview_img = Image .new ("RGB" , preview_size , tuple (int (average_color .lstrip ('#' )[i :i + 2 ], 16 ) for i in (0 , 2 , 4 )))
130
+
122
131
preview_img .paste (img , ((preview_size [0 ]- img_size [0 ])// 2 ,
123
132
(preview_size [1 ]- img_size [1 ])// 2 ))
124
133
preview_img .show ()
0 commit comments