python实现对文件中图片生成带标签的txt文件方法

yipeiwu_com5年前Python基础

在深度学习中经常需要生成带标签的图片名称列表,xxxlist.txt文件,下面写一个简单的python脚本生成该文件列表。

import os
def generate(dir,label):
  files = os.listdir(dir)
  files.sort()
  print '****************'
  print 'input :',dir
  print 'start...'
  listText = open(dir+'\\'+'list.txt','w')
  for file in files:
    fileType = os.path.split(file)
    if fileType[1] == '.txt':
      continue    
    name = file + ' ' + str(int(label)) +'\n'
    listText.write(name)
  listText.close()
  print 'down!'
  print '****************'  

if __name__ == '__main__': 
  generate('C:\\Users\\Desktop\\image',1)  

运行该脚本,会在image文件夹中生成一个list.txt文件,并且每张图片带有标签1.

以上这篇python实现对文件中图片生成带标签的txt文件方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

对matplotlib改变colorbar位置和方向的方法详解

如下所示: #! usr/bin/python #coding=utf-8 import numpy as np import matplotlib.pyplot as plt...

python脚本生成caffe train_list.txt的方法

首先给出代码: import os path = "/home/data//" path_exp = os.path.expanduser(path) classes =...

Python中的类与类型示例详解

Python中的类与类型示例详解

1.经典类与新式类 在了解Python的类与类型前,需要对Python的经典类(classic classes)与新式类(new-style classes)有个简单的概念。 在Pyth...

python下MySQLdb用法实例分析

本文实例讲述了python下MySQLdb用法。分享给大家供大家参考。具体分析如下: 下载安装MySQLdb ① linux版本 http://sourceforge.net/proje...

Python学习笔记之自定义函数用法详解

本文实例讲述了Python学习笔记之自定义函数用法。分享给大家供大家参考,具体如下: 函数能提高应用的模块性,和代码的重复利用率。Python提供了许多内建函数,比如print()等。也...