forked from mttu-developers/konabot
fximage
This commit is contained in:
51
konabot/plugins/fx_process/__init__.py
Normal file
51
konabot/plugins/fx_process/__init__.py
Normal file
@ -0,0 +1,51 @@
|
||||
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_plugin_alconna import (
|
||||
UniMessage,
|
||||
UniMsg
|
||||
)
|
||||
|
||||
filter_map = {
|
||||
"模糊": ImageFilterUtils.apply_blur
|
||||
}
|
||||
|
||||
def is_fx_mentioned(evt: BaseEvent, msg: UniMsg) -> bool:
|
||||
txt = msg.extract_plain_text()
|
||||
if "fx" not in txt[:3]:
|
||||
return False
|
||||
return True
|
||||
|
||||
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:
|
||||
return
|
||||
filter_name = args[1]
|
||||
filter_func = filter_map[filter_name]
|
||||
# 获取函数最大参数数量
|
||||
sig = signature(filter_func)
|
||||
max_params = len(sig.parameters) - 1 # 减去第一个参数 image
|
||||
# 从 args 提取参数,不能超界
|
||||
func_args = []
|
||||
for i in range(2, min(len(args), max_params + 2)):
|
||||
func_args.append(args[i])
|
||||
# 应用滤镜
|
||||
out_img = filter_func(img, *func_args)
|
||||
output = BytesIO()
|
||||
out_img.save(output, format="PNG")
|
||||
await fx_on.send(await UniMessage().image(raw=output).export())
|
||||
|
||||
15
konabot/plugins/fx_process/fx_handle.py
Normal file
15
konabot/plugins/fx_process/fx_handle.py
Normal file
@ -0,0 +1,15 @@
|
||||
from PIL import Image, ImageFilter
|
||||
|
||||
class ImageFilterUtils:
|
||||
@staticmethod
|
||||
def apply_blur(image: Image.Image, radius: float = 5) -> Image.Image:
|
||||
"""对图像应用模糊效果
|
||||
|
||||
参数:
|
||||
image: 要处理的Pillow图像对象
|
||||
radius: 模糊半径,值越大模糊效果越明显
|
||||
|
||||
返回:
|
||||
处理后的Pillow图像对象
|
||||
"""
|
||||
return image.filter(ImageFilter.GaussianBlur(radius))
|
||||
Reference in New Issue
Block a user