Files
abot/xiuren/random_pic.py
2025-03-06 17:57:58 +08:00

38 lines
1.0 KiB
Python

import os
import random
def get_random_file_from_dir(directory):
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
image_files = []
with os.scandir(directory) as entries:
for entry in entries:
if entry.is_file():
_, ext = os.path.splitext(entry.name)
if ext.lower() in image_extensions:
image_files.append(entry.path)
return random.choice(image_files) if image_files else None
def get_xiuren_pic():
# 使用示例
directory = 'xiuren' # 替换为你的目录路径
random_file_path = get_random_file_from_dir(directory)
if random_file_path:
return os.path.abspath(random_file_path)
else:
return None
def get_xiuren_heisi_pic():
# 使用示例
directory = 'xiuren/heisi' # 替换为你的目录路径
random_file_path = get_random_file_from_dir(directory)
if random_file_path:
return os.path.abspath(random_file_path)
else:
return None