提醒UI的GIF实现与参数传递
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-12-04 16:20:09 +08:00
parent eed21e6223
commit 35f411fb3a

View File

@ -1,8 +1,11 @@
from io import BytesIO from io import BytesIO
import random
from PIL import Image from PIL import Image
from konabot.common.web_render import konaweb from konabot.common.web_render import konaweb
from konabot.common.web_render.core import WebRenderer from konabot.common.web_render.core import WebRenderer
import numpy as np
from playwright.async_api import Page from playwright.async_api import Page
class NoticeUI: class NoticeUI:
@ -18,13 +21,19 @@ class NoticeUI:
# 直到 setContent 函数加载完成 # 直到 setContent 函数加载完成
await page.wait_for_function("typeof setContent === 'function'", timeout=1000) await page.wait_for_function("typeof setContent === 'function'", timeout=1000)
# 设置标题和消息内容 # 设置标题和消息内容
await page.evaluate(f'setContent("{title}", "{message}")') await page.evaluate("""([title, message]) => {
return setContent(title, message);
}""",
[title, message])
async def mask_function(page: Page): async def mask_function(page: Page):
# 直到 setContent 函数加载完成 # 直到 setContent 函数加载完成
await page.wait_for_function("typeof setContent === 'function'", timeout=1000) await page.wait_for_function("typeof setContent === 'function'", timeout=1000)
# 设置标题和消息内容 # 设置标题和消息内容
await page.evaluate(f'setContent("{title}", "{message}")') await page.evaluate("""([title, message]) => {
return setContent(title, message);
}""",
[title, message])
# 直到 setMaskMode 函数加载完成 # 直到 setMaskMode 函数加载完成
await page.wait_for_function("typeof setMaskMode === 'function'", timeout=1000) await page.wait_for_function("typeof setMaskMode === 'function'", timeout=1000)
await page.evaluate('setMaskMode(true)') await page.evaluate('setMaskMode(true)')
@ -44,33 +53,16 @@ class NoticeUI:
image = Image.open(BytesIO(image_bytes)).convert("RGBA") image = Image.open(BytesIO(image_bytes)).convert("RGBA")
mask = Image.open(BytesIO(mask_bytes)).convert("L") mask = Image.open(BytesIO(mask_bytes)).convert("L")
# 遮罩抖动二值化
# 使用mask作为alpha通道 mask = mask.convert('1') # 先转换为1位图像
r, g, b, _ = image.split() image.putalpha(mask)
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 # 保存为GIF
output_buffer = BytesIO() output_buffer = BytesIO()
palette_img.save( image.save(
output_buffer, output_buffer,
format="GIF", format="GIF",
transparency=0, # 将索引0设为透明 disposal=2
disposal=2,
loop=0
) )
output_buffer.seek(0) output_buffer.seek(0)