From 04cc2a2084754038c3f822ac717579d1fad9931d Mon Sep 17 00:00:00 2001 From: iou1name Date: Sat, 16 Dec 2017 02:10:36 -0500 Subject: [PATCH] replaced alpha composite function and renamed light.py to iot.py --- modules/ascii.py | 47 ++++++------------------------------ modules/{light.py => iot.py} | 20 +++++++++++++++ 2 files changed, 28 insertions(+), 39 deletions(-) rename modules/{light.py => iot.py} (65%) diff --git a/modules/ascii.py b/modules/ascii.py index 9143e9e..47a5e74 100755 --- a/modules/ascii.py +++ b/modules/ascii.py @@ -144,46 +144,15 @@ def open_image(imagePath): return image -def alpha_composite(front, back): - """Alpha composite two RGBA images. - +def alpha_composite(image, color=(255, 255, 255)): + """ + Alpha composite an RGBA Image with a specified color. Source: http://stackoverflow.com/a/9166671/284318 - - Keyword Arguments: - front -- PIL RGBA Image object - back -- PIL RGBA Image object - """ - front = np.asarray(front) - back = np.asarray(back) - result = np.empty(front.shape, dtype='float') - alpha = np.index_exp[:, :, 3:] - rgb = np.index_exp[:, :, :3] - falpha = front[alpha] / 255.0 - balpha = back[alpha] / 255.0 - result[alpha] = falpha + balpha * (1 - falpha) - old_setting = np.seterr(invalid='ignore') - result[rgb] = (front[rgb] * falpha + back[rgb] * balpha * (1 - falpha)) / result[alpha] - np.seterr(**old_setting) - result[alpha] *= 255 - np.clip(result, 0, 255) - # astype('uint8') maps np.nan and np.inf to 0 - result = result.astype('uint8') - result = Image.fromarray(result, 'RGBA') - return result - - -def alpha_composite_with_color(image, color=(255, 255, 255)): - """Alpha composite an RGBA image with a single color image of the - specified color and the same size as the original image. - - Keyword Arguments: - image -- PIL RGBA Image object - color -- Tuple r, g, b (default 255, 255, 255) - - """ - back = Image.new('RGBA', size=image.size, color=color + (255,)) - return alpha_composite(image, back) + image.load() # needed for split() + background = Image.new('RGB', image.size, color) + background.paste(image, mask=image.split()[3]) # 3 is the alpha channel + return background def image_to_ascii(image=None, reverse=False, brail=False, color=None,**kwargs): @@ -198,7 +167,7 @@ def image_to_ascii(image=None, reverse=False, brail=False, color=None,**kwargs): if image.mode == "P": image = image.convert(image.palette.mode) if image.mode == "RGBA": - image = alpha_composite_with_color(image).convert("RGB") + image = alpha_composite(image).convert("RGB") image = scale_image(image) diff --git a/modules/light.py b/modules/iot.py similarity index 65% rename from modules/light.py rename to modules/iot.py index 84fa6ac..9bd340c 100755 --- a/modules/light.py +++ b/modules/iot.py @@ -39,3 +39,23 @@ def roomTemp(bot, trigger): except requests.exceptions.ConnectionError: return bot.say("Connection error. Is the unit dead?") bot.say(res.text) + + +@module.require_admin +@module.commands('inkwrite') +def roomTemp(bot, trigger): + """ + Writes shit to my e-ink screen. + """ + text = trigger.replace(".inkwrite ", "") + if not text: + return bot.say("Need something to write.") + + try: + res = requests.get(f"http://192.168.1.125:8000/?text={text}", + timeout=10) + except requests.exceptions.ReadTimeout: + return bot.say("Connection error. Timeout reached.") + except requests.exceptions.ConnectionError: + return bot.say("Connection error. Is the unit dead?") + bot.say("Wrote: " + res.text)