在做项目时遇到需要标记数据集里面的若干图片数据,作为程序员,为避免手动一张一张的筛选,所以写了这个Python脚本实现。
Python脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# from PIL import Image import csv import os import shutil filename = 'img.txt' def readImageName(): with open (filename) as f: lines = f.readlines() imgnames = [] for line in lines: imgnames.append(line.strip().strip( ".jpg" )[ - 4 :]) print (imgnames) return imgnames def pickImg(): pickImageNames = readImageName() # 遍历所有图片集的文件名 for image in os.listdir(r "C:\Users\Administrator.PC-201708272051\Desktop\项目组\text_detect_label_data\China_SameBrowser" ): # print(image[:-4]) if image[: - 4 ] in pickImageNames: # pickImage = Image.open((r"C:\Users\Administrator.PC-201708272051\Desktop\项目组\text_detect_label_data\China_SameBrowser/%s") % image) # pickImage.save((r"C:/Users/Administrator.PC-201708272051/Desktop/labeldata/%s") % image) oldname = r "C:\Users\Administrator.PC-201708272051\Desktop\项目组\text_detect_label_data\China_SameBrowser/" + image newname = r "C:/Users/Administrator.PC-201708272051/Desktop/labeldata/" + image shutil.copyfile(oldname,newname) # readImageName() pickImg() |
以上这篇用Python实现筛选文件脚本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Enjoying_Science/article/details/80847500