From 5f8cda7c8a8e67ecc5168a25b268fad36e1309e7 Mon Sep 17 00:00:00 2001 From: hkc Date: Thu, 4 Jul 2024 16:37:14 +0300 Subject: [PATCH] Implemented avoidance and black'd it --- async-bot.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/async-bot.py b/async-bot.py index 86f0391..629c68e 100644 --- a/async-bot.py +++ b/async-bot.py @@ -35,8 +35,13 @@ class AsyncBotManager: for y in range(im.height): for x in range(im.width): l, a = im.getpixel((x, y)) # type: ignore + index = x + ox + (y + oy) * 1000 if a: - self.difference[x + ox + (y + oy) * 1000] = l > 0 + self.put_index(index, l > 0) + + def put_index(self, index: int, value: bool): + if not any([index in avoid for avoid in self.avoid]): + self.difference[index] = value def add_avoid_rect(self, sx: int, sy: int, w: int, h: int): for y in range(sy, sy + h): @@ -128,9 +133,13 @@ async def amain(): mgr.font = ImageFont.truetype(settings["font"], 8) for avoid in settings["avoid"]: if avoid["type"] == "rect": - mgr.add_avoid_rect(avoid["x"], avoid["y"], avoid["w"], avoid["h"]) + mgr.add_avoid_rect( + avoid["x"], avoid["y"], avoid["w"], avoid["h"] + ) elif avoid["type"] == "range": - mgr.add_avoid_range(avoid["start"], avoid["stop"], avoid["step"]) + mgr.add_avoid_range( + avoid["start"], avoid["stop"], avoid["step"] + ) for elem in settings["elements"]: if elem["type"] == "text": mgr.put_text(elem["x"], elem["y"], elem["text"]) @@ -147,9 +156,9 @@ async def amain(): continue ndx_start = (x + ox + (y + oy) * 577) * 3 - mgr.difference[ndx_start] = r > 128 - mgr.difference[ndx_start + 1] = g > 128 - mgr.difference[ndx_start + 2] = b > 128 + mgr.put_index(ndx_start, r > 128) + mgr.put_index(ndx_start + 1, g > 128) + mgr.put_index(ndx_start + 2, b > 128) elif elem["type"] == "rgb565": ox, oy = elem["x"], elem["y"] with Image.open(elem["path"]).convert("RGBA") as im: @@ -164,9 +173,9 @@ async def amain(): color |= b >> 3 for i in range(16): - mgr.difference[ndx_start + i] = ( - (color >> i) & 1 - ) > 0 + mgr.put_index( + ndx_start + i, ((color >> i) & 1) > 0 + ) mgr.get_difference_image().save("result.png") mgr.get_avoid_image().save("avoid.png")