业界有一句话,Python
除了不能生孩子,其他的都能做。
最近处理模型的热图,需要将有无损失函数的图片生成然后放到PPT
里去对比,从百十个类别中挑图就很费时间了,调完还需要贴到PPT
里,我开始是把每个类别中比较好的图片名记下来,应该当时直接复制到PPT
里的,但想着统计一下嘛,后来还得根据类名+图片名从源文件夹中挑,当然,挑了一阵子我感觉不能这么干,还得写个脚本让它自己挑
如下:
import os
import shutil
txt_path = "./contrastive.txt"
with open(txt_path, 'r', encoding='utf-8') as file:
for line in file:
print(line.strip())
split_list = line.strip().split(":")
print(split_list[0], split_list[1])
class_nu = split_list[0]
print(f"class_nu:{class_nu}")
img_name_str = split_list[1]
fmujiehot_path = "./Fmujiehot_5"
origin_path = "./valClass"
os.makedirs(os.path.join(class_nu), exist_ok=True)
img_name_list = img_name_str.split("、")
print(img_name_list)
current_class_folder_name = fmujiehot_path
class_folder_list = os.listdir(fmujiehot_path)
print(class_folder_list)
for class_folder_name in class_folder_list:
if class_folder_name.split(".")[0] == class_nu:
print(class_folder_name)
origin_class_path = os.path.join(origin_path, class_folder_name)
print(f"origin_class_path:{origin_class_path}")
current_class_folder_name = os.path.join(fmujiehot_path, class_folder_name)
break
print(current_class_folder_name)
for img_name in img_name_list:
current_img_path = os.path.join(current_class_folder_name, img_name + '.jpg')
print(f"current_img_path:{current_img_path}")
shutil.copy(current_img_path, class_nu)
origin_img_path = os.path.join(origin_class_path, img_name + '.jpg')
new_origin_img_path = origin_img_path.replace(img_name, img_name + "_origin")
# os.rename(origin_img_path, new_origin_img_path)
shutil.copy(origin_img_path, new_origin_img_path)
shutil.copy(new_origin_img_path, class_nu)
print(f"origin_img_path:{origin_img_path}")
vit_img_path = current_img_path.replace("Fmujiehot_5", "vithot_5")
print(vit_img_path)
new_vit_img_path = vit_img_path.replace(img_name, img_name + "_5")
print(f"new_vit_img_path:{new_vit_img_path}")
# os.rename(vit_img_path, new_vit_img_path)
shutil.copy(vit_img_path, new_vit_img_path)
shutil.copy(new_vit_img_path, class_nu)
很简单,就是你统计了一个txt
文件,里边有类名和图片名,根据这个文件分门别类存放就行了,少的话可以手动找,多了还是自动找吧。
找完后在文件夹里还得自己翻着查看,挺麻烦的,不如在PPT
里上下翻页方便。
所以说,就应该挑出来直接贴PPT
里,上述小插曲~
关键是 可视化方式不同,你文件夹会有好多,贴的时候头疼。特别是每个文件夹中还好多类别文件夹,每个类别文件夹下还有好几张图片,更头疼。
现在目录假设是这样的:
`valimg:.
├─Method1_hot
│ ├─cls_num_1
│ ├─cls_num_2
│ ……
│ ├─cls_num_n-1
│ └─cls_num_n
├─Method2_hot
│ ├─cls_num_1
│ ├─cls_num_2
│ ……
│ ├─cls_num_n-1
│ └─cls_num_n`
cls_num/
内就是具体的图片
现在要用python
的python-pptx
库来傻瓜式操作了,先安装
pip install python-pptx
import os
from pptx import Presentation
from pptx.util import Inches, Cm
from PIL import Image
# 创建一个新的演示文稿
prs = Presentation()
# 设置幻灯片尺寸为16:9(宽度为 33.867 cm,高度为 19.05 cm)
slide_width = Inches(13.333) # 33.867 cm
slide_height = Inches(7.5) # 19.05 cm
prs.slide_width = slide_width # 33.867 cm
prs.slide_height = slide_height # 19.05 cm
def resize_image_to_slide(image_path, slide_width, slide_height):
# 打开图片并获取原始尺寸
with Image.open(image_path) as img:
img_width, img_height = img.size
# 计算宽高比
img_ratio = img_width / img_height
slide_ratio = slide_width / slide_height
# 计算缩放比例
if img_ratio > slide_ratio:
new_width = slide_width
new_height = new_width / img_ratio
else:
new_height = slide_height
new_width = new_height * img_ratio
return new_width, new_height
# 文件夹路径
folder_base_path = r"./valimg/"
folder_name_list = ["Method1_hot", "Method2_hot", "Method3_hot", "Method4_hot"]
each_folder_base_path = os.path.join(folder_base_path, folder_name_list[0])
# 获取文件夹下的类别名称
for each_cls_folder_name in os.listdir(each_folder_base_path):
# 获得类别目录
each_cls_path = os.path.join(each_folder_base_path, each_cls_folder_name)
# 获得类别目录下的文件列表
for img_file in os.listdir(each_cls_path):
if img_file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
# 启用占位符辅助
auxiliary_each_cls_path = each_cls_path.replace(folder_name_list[0], "placeholder")
for img_type in folder_name_list:
# 添加幻灯片
slide_layout = prs.slide_layouts[5] # 使用空白布局
slide = prs.slides.add_slide(slide_layout)
# 获得图片路径
img_path = os.path.join(auxiliary_each_cls_path, img_file)
# 根据不同文件夹更改路径str
img_path = img_path.replace("placeholder", img_type)
# 获取调整后的图片尺寸
new_width, new_height = resize_image_to_slide(img_path, slide_width, slide_height)
# 计算图片在幻灯片上的位置(居中)
left = (slide_width - new_width) / 2
top = (slide_height - new_height) / 2
pic = slide.shapes.add_picture(img_path, left, top, width=new_width, height=new_height)
# 保存演示文稿
prs.save('./output_presentation.pptx')
这样按照顺序,有无损失函数+不同的可视化方法直接放一块,几秒钟150+张图片,比手动进不同文件夹去Ctrl+C/V
快了N
倍,虽然写代码会费点时间,但是后续还需要其他数据集的热图对比,这样看,效率高多了。
虽然能够不用鼠标,光靠键盘快捷键就可以去不同文件夹下找找图片复制粘贴,但这种傻瓜式操作真的累,手能给你按抽筋,别问我咋知道的,高强度固定几个键位高频按就是会这样~
自动化深得我心~~
还不快抢沙发