Skip to content

Commit f09e27b

Browse files
authored
Fix RGBA issue (#39)
1 parent 56edb33 commit f09e27b

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

apps/media_lights_sync/media_lights_sync.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def change_lights_color(self, entity, attribute, old_url, new_url, kwargs):
5050
# Image already processed from another callback
5151
return self.log(log_message.format(entity=entity, attr=attribute+"; skipped"))
5252
self.log(log_message.format(entity=entity, attr=attribute))
53-
53+
5454
try:
5555
url = self.format_url(new_url, entity, attribute)
5656
rgb_colors = self.get_colors(url)
@@ -112,9 +112,18 @@ def get_colors(self, url):
112112
fd = urlopen(url)
113113
f = io.BytesIO(fd.read())
114114
im = Image.open(f)
115+
if im.mode == "RGBA" and self.quantization_method not in [None, Image.FASTOCTREE, Image.LIBIMAGEQUANT]:
116+
im = self.convert_rgba_to_rgb(im)
117+
115118
palette = im.quantize(colors=len(self.lights), method=self.quantization_method).getpalette()
116119
return self.extract_colors(palette, len(self.lights))
117-
120+
121+
def convert_rgba_to_rgb(self, rgba_image):
122+
rgba_image.load() # required for png.split()
123+
rgb_image = Image.new("RGB", rgba_image.size, (255, 255, 255))
124+
rgb_image.paste(rgba_image, mask=rgba_image.split()[3]) # 3 is the alpha channel
125+
return rgb_image
126+
118127
def get_quantization_method(self, value):
119128
method = None
120129
if value == "FastOctree":
@@ -128,8 +137,8 @@ def get_quantization_method(self, value):
128137
method = Image.LIBIMAGEQUANT
129138
else:
130139
self.log("Quantization method 'libimagequant' is unsupported by your platform.")
131-
self.log("Using {method} quantization method".format(method="default (MedianCut)" if method is None else value))
132-
return Image.MEDIANCUT if method is None else method
140+
self.log("Using {method} quantization method".format(method="default" if method is None else value))
141+
return method
133142

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

0 commit comments

Comments
 (0)