From 2d630a771cfd2f58ee66343fdc180b4b88343164 Mon Sep 17 00:00:00 2001 From: zim514 Date: Sun, 7 Jan 2024 10:57:07 -0500 Subject: [PATCH] Revert "Minor ambilight optimization" This reverts commit cd54c42991e845be3dde9b77ffca4232de5bd488. --- script.service.hue/resources/lib/imageprocess.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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