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 通过 socket 发送文件的实例代码

python 通过 socket 发送文件的实例代码

目录结构: client: #!/usr/bin/env python # -*-coding:utf-8 -*- import socket, struct, json down...

python 接口实现 供第三方调用的例子

python 接口实现 供第三方调用的例子

实验环境 1.环境问题 python 2.7 以上自带的pyunit bottle 作为一个python的简易服务器 在python安装目录 打开命令窗口(具体 shift+鼠标右键)...

Django实现表单验证

本文实例为大家分享了Django实现表单验证的具体代码,供大家参考,具体内容如下 models.py class Users(models.Model): nickname =...

Python绘制三角函数图(sin\cos\tan)并标注特定范围的例子

Python绘制三角函数图(sin\cos\tan)并标注特定范围的例子

根据我们指定的条件检索函数中的元素 import matplotlib.pyplot as plt import numpy as np a = np.linspace(0, 2...

老生常谈Python基础之字符编码

老生常谈Python基础之字符编码

前言 字符编码非常容易出问题,我们要牢记几句话: 1.用什么编码保存的,就要用什么编码打开 2.程序的执行,是先将文件读入内存中 3.unicode是父编码,只能encode解码成其他编...