批量修改大量文本文件(或其他文件)里的内容(图片标注XML文件里的内容修改)

本文介绍了一种使用VBS脚本将目录中所有PNG格式的图片批量转换为JPG格式的方法。通过创建并运行VBS文件,可以实现对大量图片格式的快速转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将png格式改为jpg格式,VBS方案:

Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run "cmd /c dir /s/b *.xml> list.xml",vbHide
Wscript.Sleep 1000

sFile = "list.xml"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = objFSO.OpenTextFile(sFile,1)
Do While Not oFile.AtEndOfStream
strLine = oFile.ReadLine
If Len(strLine) > 0 Then
Set File = objFSO.OpenTextFile(strLine, 1)
aryLines = File.ReadAll
File.Close
aryLines = Replace(aryLines, "png", "jpg")
Set File = objFSO.OpenTextFile(strLine, 2)
File.Write aryLines
File.Close
End If
Loop
oFile.Close

objFSO.DeleteFile sFile
Set objFSO = Nothing

保存为VBS文件,放到TXT文件目录,双击执行。

### 将标注XML 文件转换为 YOLO 格式的 TXT 文件 为了实现从 VOC 标注格式到 YOLO 格式的转换,可以编写 Python 脚本来处理此任务。该脚本会读取指定目录中的 `.xml` 文件,并将其转换成对应的 `.txt` 文件,同时生成 `classes.txt` 文件来记录所有的类别名称。 #### 函数定义 首先定义函数用于解析单个 XML 文件并写入相应的 YOLO 格式标签文件: ```python import os from xml.etree import ElementTree as ET def convert(size, box): dw = 1./(size[0]) dh = 1./(size[1]) x = (box[0] + box[1])/2.0 - 1 y = (box[2] + box[3])/2.0 - 1 w = box[1] - box[0] h = box[3] - box[2] x = x*dw w = w*dw y = y*dh h = h*dh return (x,y,w,h) def xml_to_yolo(xml_file, classes, output_txt): tree = ET.parse(xml_file) root = tree.getroot() image_width = int(root.find('size/width').text) image_height = int(root.find('size/height').text) with open(output_txt, "w") as f: for obj in root.iter('object'): difficult = obj.find('difficult').text cls = obj.find('name').text if cls not in classes or int(difficult)==1: continue cls_id = classes.index(cls) bndbox = obj.find('bndbox') bbox = ( float(bndbox.find('xmin').text), float(bndbox.find('xmax').text), float(bndbox.find('ymin').text), float(bndbox.find('ymax').text) ) bb = convert((image_width, image_height), bbox) f.write(f"{cls_id} {' '.join([str(a) for a in bb])}\n") ``` 上述代码实现了将给定的 XML 文件按照 YOLO 的标准格式写出至文本文件的功能[^1]。 #### 主程序逻辑 接下来设置主程序入口,在这指定了输入输出路径以及类别的列表: ```python if __name__ == '__main__': postfix = 'jpg' # 图像文件扩展名可根据实际情况调整 imgpath = r'/path/to/images' xmlpath = r'/path/to/xmls' txtpath = r'/output/path/txt' class_names = ['class1', 'class2'] # 替换为目标数据集的实际分类 # 如果输出路径不存在,则创建它 if not os.path.exists(txtpath): os.makedirs(txtpath) # 创建 classes.txt 文件 with open(os.path.join(txtpath, 'classes.txt'), 'w') as f: for c in class_names: f.write(c + '\n') # 处理每一个 .xml 文件 for xml_name in os.listdir(xmlpath): if xml_name.endswith('.xml'): xml_full_path = os.path.join(xmlpath, xml_name) txt_full_path = os.path.join(txtpath, os.path.splitext(xml_name)[0]+'.txt') try: xml_to_yolo(xml_full_path, class_names, txt_full_path) except Exception as e: print(f"Error processing {xml_name}: {e}") ``` 这段代码遍历了所有 `.xml` 文件并将它们逐个传递给之前定义好的转换函数,同时也负责生成包含所有类别的 `classes.txt` 文件][^[^23]。 通过这种方式就可以高效地批量完成由 Pascal VOC 到 YOLO 数据格式之间的转换工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值