用Python实现筛选文件脚本的方法

yipeiwu_com6年前Python基础

在做项目时遇到需要标记数据集里面的若干图片数据,作为程序员,为避免手动一张一张的筛选,所以写了这个Python脚本实现。

Python脚本如下:

# 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实现筛选文件脚本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

go和python变量赋值遇到的一个问题

平时写得多的是python,最近看了一点go,今天碰到了一个问题,和大家分享一下 package main import "fmt" type student struct {...

python机器学习库常用汇总

汇总整理一套Python网页爬虫,文本处理,科学计算,机器学习和数据挖掘的兵器谱。 1. Python网页爬虫工具集 一个真实的项目,一定是从获取数据开始的。无论文本处理,机器学习和数据...

Python键盘输入转换为列表的实例

Python输入字符串转列表是为了方便后续处理,这种操作在考试的时候比较多见。 1.在Python3.0以后,键盘输入使用input函数 eg1. >>> x=in...

python3 assert 断言的使用详解 (区别于python2)

python3 和python以前的版本有点不同 如果你断言的 语句正确 则什么反应都没有 但是如果你出错之后 就会报出 AssertionError 并且错误可以自己填写 格式 :...

对python xlrd读取datetime类型数据的方法详解

使用xlrd读取出来的时间字段是类似41410.5083333的浮点数,在使用时需要转换成对应的datetime类型,下面代码是转换的方法: 首先需要引入xldate_as_tuple函...