zhenxun_bot/plugins/image_management/send_image/anti.py

27 lines
671 B
Python
Raw Normal View History

2023-07-29 09:53:44 +08:00
import random
import warnings
2023-08-18 00:42:10 +08:00
from pathlib import Path
import cv2
2023-07-29 09:53:44 +08:00
import numpy as np
from PIL import Image
2023-08-17 22:37:50 +08:00
def pix_random_change(img):
2023-07-29 09:53:44 +08:00
# Image转cv2
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
2023-08-18 00:42:10 +08:00
img[0, 0, 0] = random.randint(0, 0xFFFFFFF)
2023-07-29 09:53:44 +08:00
# cv2转Image
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
return img
2023-08-18 00:42:10 +08:00
def pix_random_change_file(path: Path):
2023-07-29 09:53:44 +08:00
# 注意cv2.imread()不支持路径中文
2023-08-18 00:42:10 +08:00
str_path = str(path.absolute())
2023-07-29 09:53:44 +08:00
warnings.filterwarnings("ignore", category=Warning)
2023-08-18 00:42:10 +08:00
img = cv2.imread(str_path)
img[0, 0, 0] = random.randint(0, 0xFFFFFFF)
cv2.imwrite(str_path, img)
return str_path