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

yipeiwu_com6年前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设计】。

相关文章

python正则表达式re模块详解

快速入门 import re pattern = 'this' text = 'Does this text match the pattern?' match = re...

PyQt5实现从主窗口打开子窗口的方法

PyQt5实现从主窗口打开子窗口的方法

1.在Qt Designer中设计两个简单窗口 2.将.ui文件转换成.py文件 3.新建**.py文件 #-*- coding:utf-8 -*- from PyQt5.QtWi...

进一步探究Python的装饰器的运用

装饰器在 python 中用的相当广泛,如果你用过 python 的一些 web 框架,那么一定对其中的 “ route() 装饰器” 不陌生,今天咱们再看一个具体的案例。 咱们来模拟一...

python解析中国天气网的天气数据

使用方法:terminal中输入复制代码 代码如下:python weather.py http://www.weather.com.cn/weather/101010100.shtml...

举例讲解Python编程中对线程锁的使用

锁 python的内置数据结构比如列表和字典等是线程安全的,但是简单数据类型比如整数和浮点数则不是线程安全的,要这些简单数据类型的通过操作,就需要使用锁。 #!/usr/bin/en...