forked from mttu-developers/konabot
fx 完善
This commit is contained in:
@ -1,25 +1,17 @@
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
from inspect import signature
|
||||
|
||||
from konabot.common.nb.extract_image import DepPILImage
|
||||
from konabot.common.nb.match_keyword import match_keyword
|
||||
from konabot.plugins.fx_process.fx_handle import ImageFilterUtils
|
||||
|
||||
import nonebot
|
||||
from nonebot.adapters import Event as BaseEvent
|
||||
from nonebot import on_message, logger
|
||||
from nonebot_plugin_alconna import Alconna, Args, on_alconna
|
||||
from nonebot import on_message
|
||||
|
||||
from nonebot_plugin_alconna import (
|
||||
UniMessage,
|
||||
UniMsg
|
||||
)
|
||||
|
||||
filter_map = {
|
||||
"模糊": ImageFilterUtils.apply_blur
|
||||
}
|
||||
from konabot.plugins.fx_process.fx_manager import ImageFilterManager
|
||||
|
||||
def is_fx_mentioned(evt: BaseEvent, msg: UniMsg) -> bool:
|
||||
txt = msg.extract_plain_text()
|
||||
@ -32,17 +24,32 @@ fx_on = on_message(rule=is_fx_mentioned)
|
||||
@fx_on.handle()
|
||||
async def _(msg: UniMsg, event: BaseEvent, img: DepPILImage):
|
||||
args = msg.extract_plain_text().split()
|
||||
if len(args) < 2 or args[1] not in filter_map:
|
||||
if len(args) < 2:
|
||||
return
|
||||
filter_name = args[1]
|
||||
filter_func = filter_map[filter_name]
|
||||
filter_func = ImageFilterManager.get_filter(filter_name)
|
||||
if not filter_func:
|
||||
return
|
||||
# 获取函数最大参数数量
|
||||
sig = signature(filter_func)
|
||||
max_params = len(sig.parameters) - 1 # 减去第一个参数 image
|
||||
# 从 args 提取参数,不能超界
|
||||
# 从 args 提取参数,并转换为适当类型
|
||||
func_args = []
|
||||
for i in range(2, min(len(args), max_params + 2)):
|
||||
func_args.append(args[i])
|
||||
# 尝试将参数转换为函数签名中对应的类型
|
||||
param = list(sig.parameters.values())[i - 1]
|
||||
param_type = param.annotation
|
||||
arg_value = args[i]
|
||||
try:
|
||||
if param_type is float:
|
||||
converted_value = float(arg_value)
|
||||
elif param_type is int:
|
||||
converted_value = int(arg_value)
|
||||
else:
|
||||
converted_value = arg_value
|
||||
except Exception:
|
||||
converted_value = arg_value
|
||||
func_args.append(converted_value)
|
||||
# 应用滤镜
|
||||
out_img = filter_func(img, *func_args)
|
||||
output = BytesIO()
|
||||
|
||||
Reference in New Issue
Block a user