Skip to content

Commit

Permalink
Fix RGBA issue (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmatte authored Feb 23, 2021
1 parent 56edb33 commit f09e27b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions apps/media_lights_sync/media_lights_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def change_lights_color(self, entity, attribute, old_url, new_url, kwargs):
# Image already processed from another callback
return self.log(log_message.format(entity=entity, attr=attribute+"; skipped"))
self.log(log_message.format(entity=entity, attr=attribute))

try:
url = self.format_url(new_url, entity, attribute)
rgb_colors = self.get_colors(url)
Expand Down Expand Up @@ -112,9 +112,18 @@ def get_colors(self, url):
fd = urlopen(url)
f = io.BytesIO(fd.read())
im = Image.open(f)
if im.mode == "RGBA" and self.quantization_method not in [None, Image.FASTOCTREE, Image.LIBIMAGEQUANT]:
im = self.convert_rgba_to_rgb(im)

palette = im.quantize(colors=len(self.lights), method=self.quantization_method).getpalette()
return self.extract_colors(palette, len(self.lights))


def convert_rgba_to_rgb(self, rgba_image):
rgba_image.load() # required for png.split()
rgb_image = Image.new("RGB", rgba_image.size, (255, 255, 255))
rgb_image.paste(rgba_image, mask=rgba_image.split()[3]) # 3 is the alpha channel
return rgb_image

def get_quantization_method(self, value):
method = None
if value == "FastOctree":
Expand All @@ -128,8 +137,8 @@ def get_quantization_method(self, value):
method = Image.LIBIMAGEQUANT
else:
self.log("Quantization method 'libimagequant' is unsupported by your platform.")
self.log("Using {method} quantization method".format(method="default (MedianCut)" if method is None else value))
return Image.MEDIANCUT if method is None else method
self.log("Using {method} quantization method".format(method="default" if method is None else value))
return method

def extract_colors(self, palette, colors):
"""Extract an amount of colors corresponding to the amount of lights in the configuration."""
Expand Down

0 comments on commit f09e27b

Please sign in to comment.