秀人图片插件化改造完成。
This commit is contained in:
@@ -124,20 +124,36 @@ class XiurenImagePlugin(MessagePluginInterface):
|
||||
if not os.path.exists(self.image_folder):
|
||||
self.LOG.error(f"图片文件夹不存在: {self.image_folder}")
|
||||
return None
|
||||
|
||||
image_files = [f for f in os.listdir(self.image_folder)
|
||||
if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp'))]
|
||||
|
||||
if not image_files:
|
||||
self.LOG.error("图片文件夹中没有图片")
|
||||
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
|
||||
image_files = []
|
||||
if not os.path.exists(self.image_folder):
|
||||
print(f"Error: Directory '{self.image_folder}' does not exist.")
|
||||
return None
|
||||
|
||||
if not os.access(self.image_folder, os.R_OK):
|
||||
print(f"Error: No read access to directory '{self.image_folder}'.")
|
||||
return None
|
||||
|
||||
print(f"Scanning directory: {self.image_folder} (including subdirectories)")
|
||||
|
||||
# 使用 os.walk() 递归遍历所有子目录
|
||||
for root, _, files in os.walk(self.image_folder):
|
||||
for file in files:
|
||||
_, ext = os.path.splitext(file)
|
||||
if ext.lower() in image_extensions:
|
||||
full_path = os.path.join(root, file)
|
||||
image_files.append(full_path)
|
||||
|
||||
if not image_files:
|
||||
print("No image files found in the directory (including subdirectories).")
|
||||
return None
|
||||
|
||||
# 随机选择一张图片
|
||||
random_pic = random.choice(image_files)
|
||||
pic_path = os.path.join(self.image_folder, random_pic)
|
||||
|
||||
return pic_path
|
||||
|
||||
if random_pic:
|
||||
return os.path.abspath(random_pic)
|
||||
else:
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"获取随机图片出错: {e}")
|
||||
return None
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user