diff --git a/script.service.hue/resources/lib/imageprocess.py b/script.service.hue/resources/lib/imageprocess.py index 9b9a40b0..29082a7d 100644 --- a/script.service.hue/resources/lib/imageprocess.py +++ b/script.service.hue/resources/lib/imageprocess.py @@ -8,6 +8,7 @@ # http://www.screenbloom.com/ from PIL import ImageEnhance + from . import timer @@ -22,7 +23,7 @@ def __init__(self): def img_avg(self, img, min_bri, max_bri, saturation): dark_pixels = 1 mid_range_pixels = 1 - total_pixels = img.width * img.height + total_pixels = 1 r = 1 g = 1 b = 1 @@ -37,7 +38,10 @@ def img_avg(self, img, min_bri, max_bri, saturation): # img.save(savepath) # ======================================================================= - for red, green, blue, alpha in img.getdata(): + # Create list of pixels + pixels = list(img.getdata()) + + for red, green, blue, alpha in pixels: # Don't count pixels that are too dark if red < self.LOW_THRESHOLD and green < self.LOW_THRESHOLD and blue < self.LOW_THRESHOLD: dark_pixels += 1 @@ -51,10 +55,12 @@ def img_avg(self, img, min_bri, max_bri, saturation): r += red g += green b += blue + total_pixels += 1 - r_avg = r / total_pixels - g_avg = g / total_pixels - b_avg = b / total_pixels + n = len(pixels) + r_avg = r / n + g_avg = g / n + b_avg = b / n rgb = [r_avg, g_avg, b_avg] # If computed average below darkness threshold, set to the threshold