replaced alpha composite function and renamed light.py to iot.py

This commit is contained in:
iou1name 2017-12-16 02:10:36 -05:00
parent f3a7dc9b80
commit 04cc2a2084
2 changed files with 28 additions and 39 deletions

View File

@ -144,46 +144,15 @@ def open_image(imagePath):
return image return image
def alpha_composite(front, back): def alpha_composite(image, color=(255, 255, 255)):
"""Alpha composite two RGBA images. """
Alpha composite an RGBA Image with a specified color.
Source: http://stackoverflow.com/a/9166671/284318 Source: http://stackoverflow.com/a/9166671/284318
Keyword Arguments:
front -- PIL RGBA Image object
back -- PIL RGBA Image object
""" """
front = np.asarray(front) image.load() # needed for split()
back = np.asarray(back) background = Image.new('RGB', image.size, color)
result = np.empty(front.shape, dtype='float') background.paste(image, mask=image.split()[3]) # 3 is the alpha channel
alpha = np.index_exp[:, :, 3:] return background
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)
def image_to_ascii(image=None, reverse=False, brail=False, color=None,**kwargs): 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": if image.mode == "P":
image = image.convert(image.palette.mode) image = image.convert(image.palette.mode)
if image.mode == "RGBA": if image.mode == "RGBA":
image = alpha_composite_with_color(image).convert("RGB") image = alpha_composite(image).convert("RGB")
image = scale_image(image) image = scale_image(image)

View File

@ -39,3 +39,23 @@ def roomTemp(bot, trigger):
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
return bot.say("Connection error. Is the unit dead?") return bot.say("Connection error. Is the unit dead?")
bot.say(res.text) 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)