- 遍历一系列日期命名的文件夹。
- 在每个日期文件夹中找到包含照片的文件夹。
- 从每个照片文件夹中读取PNG文件和相应的打分TXT文件。
- 针对每个分数阈值(从0.01到0.50,以0.01为间隔),计算分数大于等于该阈值的照片数量,然后选择使得照片数量最大化的最优阈值。
- 输出最优阈值,并将分数大于等于和小于该阈值的优秀照片路径保存并打印下来。
- 计算分数大于等于和小于该阈值的优秀照片的比例。
- 将小于最优阈值的图片复制到你设定的文件夹中,并将该照片对应文件夹的txt里的分数标注在文件名的最开始部分。
import os
import shutil
def read_score(txt_file):
with open(txt_file, 'r') as file:
score = float(file.readline().strip())
return score
def count_excellent_photos(png_files, txt_files, threshold):
excellent_paths = {'above_threshold': [], 'below_threshold': []}
above_threshold_count = 0
below_threshold_count = 0
for png_file, txt_file in zip(png_files, txt_files):
score = read_score(txt_file)
if score >= threshold:
excellent_paths['above_threshold'].append(png_file)
above_threshold_count += 1
else:
excellent_paths['below_threshold'].append(png_file)
below_threshold_count += 1
return excellent_paths, above_threshold_count, below_threshold_count
def find_optimal_threshold(root_dir):
optimal_threshold = 0
max_excellent_count = 0
total_photos = 0
for dirpath, _, _ in os.walk(root_dir):
png_files = [f for f in os.listdir(dirpath) if f.endswith('.png')]
txt_files = [f for f in os.listdir(dirpath) if f.endswith('.txt')]
total_photos += len(png_files)
if png_files and txt_files:
for threshold in range(1, 51):
threshold_value = threshold / 100
excellent_paths, above_count, below_count = count_excellent_photos(png_files, txt_files, threshold_value)
if above_count > max_excellent_count:
max_excellent_count = above_count
optimal_threshold = threshold_value
optimal_paths = excellent_paths
return optimal_threshold, optimal_paths, max_excellent_count, total_photos
def move_below_threshold_photos(root_dir, dest_dir, threshold):
for dirpath, _, _ in os.walk(root_dir):
png_files = [f for f in os.listdir(dirpath) if f.endswith('.png')]
txt_files = [f for f in os.listdir(dirpath) if f.endswith('.txt')]
if png_files and txt_files:
for png_file, txt_file in zip(png_files, txt_files):
score = read_score(os.path.join(dirpath, txt_file))
if score < threshold:
filename, extension = os.path.splitext(png_file)
new_filename = f"{score}_{filename}{extension}"
shutil.copy(os.path.join(dirpath, png_file), os.path.join(dest_dir, new_filename))
def main():
root_dir = '/path/to/your/source/directory'
dest_dir = '/path/to/your/destination/directory'
optimal_threshold, optimal_paths, max_excellent_count, total_photos = find_optimal_threshold(root_dir)
print("Optimal Threshold:", optimal_threshold)
print("Number of excellent photos above threshold:", max_excellent_count)
print("Total number of photos:", total_photos)
print("Ratio of excellent photos above threshold:", max_excellent_count / total_photos)
print("Ratio of excellent photos below threshold:", (total_photos - max_excellent_count) / total_photos)
print("\nExcellent Photos Above Threshold:")
for path in optimal_paths['above_threshold']:
print(path)
print("\nExcellent Photos Below Threshold:")
for path in optimal_paths['below_threshold']:
print(path)
move_below_threshold_photos(root_dir, dest_dir, optimal_threshold)
if __name__ == "__main__":
main()
import os
import glob
import shutil
def read_score(txt_file):
with open(txt_file, 'r') as file:
score = float(file.readline().strip())
return score
def count_excellent_photos(png_files, txt_files, threshold):
excellent_paths = {'above_threshold': [], 'below_threshold': []}
above_threshold_count = 0
below_threshold_count = 0
for png_file, txt_file in zip(png_files, txt_files):
score = read_score(txt_file)
if score >= threshold:
excellent_paths['above_threshold'].append(png_file)
above_threshold_count += 1
else:
excellent_paths['below_threshold'].append(png_file)
below_threshold_count += 1
return excellent_paths, above_threshold_count, below_threshold_count
def find_optimal_threshold(root_dir):
optimal_threshold = 0
max_excellent_count = 0
total_photos = 0
for dirpath, _, _ in os.walk(root_dir):
png_files = glob.glob(os.path.join(dirpath, '*.png'))
txt_files = glob.glob(os.path.join(dirpath, '*.txt'))
total_photos += len(png_files)
if png_files and txt_files:
for threshold in range(1, 51):
threshold_value = threshold / 100
excellent_paths, above_count, below_count = count_excellent_photos(png_files, txt_files, threshold_value)
if above_count > max_excellent_count:
max_excellent_count = above_count
optimal_threshold = threshold_value
optimal_paths = excellent_paths
return optimal_threshold, optimal_paths, max_excellent_count, total_photos
def move_below_threshold_photos(root_dir, dest_dir, threshold):
for dirpath, _, _ in os.walk(root_dir):
png_files = glob.glob(os.path.join(dirpath, '*.png'))
txt_files = glob.glob(os.path.join(dirpath, '*.txt'))
if png_files and txt_files:
for png_file, txt_file in zip(png_files, txt_files):
score = read_score(txt_file)
if score < threshold:
filename = os.path.basename(png_file)
foldername = os.path.basename(os.path.dirname(png_file))
new_filename = f"{score}_{filename}"
new_path = os.path.join(dest_dir, foldername + '_' + new_filename)
shutil.copy(png_file, new_path)
def main():
root_dir = '/path/to/your/source/directory'
dest_dir = '/path/to/your/destination/directory'
optimal_threshold, optimal_paths, max_excellent_count, total_photos = find_optimal_threshold(root_dir)
print("Optimal Threshold:", optimal_threshold)
print("Number of excellent photos above threshold:", max_excellent_count)
print("Total number of photos:", total_photos)
print("Ratio of excellent photos above threshold:", max_excellent_count / total_photos)
print("Ratio of excellent photos below threshold:", (total_photos - max_excellent_count) / total_photos)
print("\nExcellent Photos Above Threshold:")
for path in optimal_paths['above_threshold']:
print(path)
print("\nExcellent Photos Below Threshold:")
for path in optimal_paths['below_threshold']:
print(path)
move_below_threshold_photos(root_dir, dest_dir, optimal_threshold)
if __name__ == "__main__":
main()