refactor🎨: 优化图库

This commit is contained in:
HibiKier
2023-08-18 00:42:10 +08:00
parent 48b720b00a
commit 38abb74c4c
6 changed files with 38 additions and 25 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import time
from typing import Any, Tuple
from nonebot import on_regex
from nonebot.adapters.onebot.v11 import Bot, MessageEvent
from nonebot.adapters.onebot.v11 import MessageEvent
from nonebot.params import RegexGroup
from configs.config import Config
+18 -12
View File
@@ -1,18 +1,21 @@
import os
import random
from typing import List
from nonebot import on_message, on_regex
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
from nonebot.adapters.onebot.v11 import Message, MessageEvent
from nonebot.params import CommandArg
from configs.config import Config
from configs.path_config import IMAGE_PATH
from services.log import logger
from utils.depends import GetConfig
from utils.manager import withdraw_message_manager
from utils.message_builder import image
from utils.utils import FreqLimiter, cn2py, get_message_text, is_number
from .rule import rule
from .anti import pix_random_change_file
from .rule import rule
__zx_plugin_name__ = "本地图库"
__plugin_usage__ = f"""
@@ -51,9 +54,11 @@ _path = IMAGE_PATH / "image_management"
@send_img.handle()
async def _(event: MessageEvent):
image_dir_list = Config.get_config("image_management", "IMAGE_DIR_LIST") or []
msg = get_message_text(event.json()).split()
async def _(
event: MessageEvent,
image_dir_list: List[str] = GetConfig("image_management", "IMAGE_DIR_LIST", []),
):
msg = get_message_text(event.message).split()
gallery = msg[0]
if gallery not in image_dir_list:
return
@@ -67,17 +72,15 @@ async def _(event: MessageEvent):
else:
path.mkdir(parents=True, exist_ok=True)
length = len(os.listdir(path))
if length == 0:
if not length:
logger.warning(f"图库 {cn2py(gallery)} 为空,调用取消!")
await send_img.finish("该图库中没有图片噢")
await send_img.finish("该图库中没有图片噢...")
index = img_id if img_id else str(random.randint(0, length - 1))
if not is_number(index):
return
if int(index) > length - 1 or int(index) < 0:
await send_img.finish(f"超过当前上下限!({length - 1})")
abs_path = os.path.join(os.getcwd(), path / f"{index}.jpg")
pix_random_change_file(abs_path)
result = image(path / f"{index}.jpg")
await send_img.finish(f"超过当前图库的上下限了哦!({length - 1})")
result = image(pix_random_change_file(path / f"{index}.jpg"))
if result:
logger.info(
f"发送{cn2py(gallery)}:" + str(path / f"{index}.jpg"),
@@ -97,7 +100,7 @@ async def _(event: MessageEvent):
)
else:
logger.info(
f"发送 {cn2py(gallery)} 失败",
f"发送 {cn2py(gallery)} 不存在",
"发送图片",
event.user_id,
getattr(event, "group_id", None),
@@ -109,6 +112,9 @@ async def _(event: MessageEvent):
async def _(event: MessageEvent):
if _flmt.check(event.user_id):
_flmt.start_cd(event.user_id)
path = IMAGE_PATH / "pa"
if not path.exists() or not os.listdir(IMAGE_PATH / "pa"):
await pa_reg.finish("该图库中没有图片噢...")
await pa_reg.finish(
image(IMAGE_PATH / "pa" / random.choice(os.listdir(IMAGE_PATH / "pa")))
)
+10 -7
View File
@@ -1,6 +1,8 @@
import cv2
import random
import warnings
from pathlib import Path
import cv2
import numpy as np
from PIL import Image
@@ -8,16 +10,17 @@ from PIL import Image
def pix_random_change(img):
# Image转cv2
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
img[0, 0, 0] = random.randint(0, 0xfffffff)
img[0, 0, 0] = random.randint(0, 0xFFFFFFF)
# cv2转Image
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
return img
def pix_random_change_file(path: str):
def pix_random_change_file(path: Path):
# 注意:cv2.imread()不支持路径中文
str_path = str(path.absolute())
warnings.filterwarnings("ignore", category=Warning)
img = cv2.imread(path)
img[0, 0, 0] = random.randint(0, 0xfffffff)
cv2.imwrite(path, img)
img = cv2.imread(str_path)
img[0, 0, 0] = random.randint(0, 0xFFFFFFF)
cv2.imwrite(str_path, img)
return str_path