From eed21e62231ea9c948ebc3c423d79bd56e58f3d7 Mon Sep 17 00:00:00 2001 From: MixBadGun <1059129006@qq.com> Date: Wed, 3 Dec 2025 22:38:50 +0800 Subject: [PATCH] new --- konabot/plugins/notice_ui/notice.py | 30 ++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/konabot/plugins/notice_ui/notice.py b/konabot/plugins/notice_ui/notice.py index 431249c..8a5e7c0 100644 --- a/konabot/plugins/notice_ui/notice.py +++ b/konabot/plugins/notice_ui/notice.py @@ -44,10 +44,34 @@ class NoticeUI: image = Image.open(BytesIO(image_bytes)).convert("RGBA") mask = Image.open(BytesIO(mask_bytes)).convert("L") - # 应用遮罩 - image.putalpha(mask) + # 使用mask作为alpha通道 + r, g, b, _ = image.split() + transparent_image = Image.merge("RGBA", (r, g, b, mask)) + + # 先创建一个纯白色背景,然后粘贴透明图像 + background = Image.new("RGBA", transparent_image.size, (255, 255, 255, 255)) + composite = Image.alpha_composite(background, transparent_image) + + palette_img = composite.convert("RGB").convert( + "P", + palette=Image.Palette.WEB, + colors=256, + dither=Image.Dither.NONE + ) + + # 将alpha值小于128的设为透明 + alpha_mask = mask.point(lambda x: 0 if x < 128 else 255) + + # 保存为GIF output_buffer = BytesIO() - image.save(output_buffer, format="GIF", disposal=2, loop=0) + palette_img.save( + output_buffer, + format="GIF", + transparency=0, # 将索引0设为透明 + disposal=2, + loop=0 + ) + output_buffer.seek(0) return output_buffer.getvalue() \ No newline at end of file