您的位置:首页 > 编程语言 > Python开发

Python批量修改图片名字,深度学习处理数据必备

2019-01-10 14:34 447 查看
版权声明:原创文章转载需注明出处 https://blog.csdn.net/yangxinquan123/article/details/86231639
[code]# -*- coding:utf8 -*-
import os

class BatchRename():
'''批量重命名文件夹中的图片文件'''
def __init__(self):
self.path = '/home/yxq/face_recognition/face-login/negative/'

def rename(self):
filelist = os.listdir(self.path)
total_sum = len(filelist)
i = 1

for item in filelist:
# endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。
if item.endswith('.jpg'):
# os.path.join 用于路径拼接,src为完整图片路径
src = os.path.join(os.path.abspath(self.path),item)
str1 = str(i)
# Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0
#dst = os.path.join(os.path.abspath(self.path),str1.zfill(6)+'.jpg')
dst = os.path.join(os.path.abspath(self.path), str1 + '.jpg')
try:
os.rename(src,dst)
print('coverting %s to %s' % (src,dst))
i = i+1
except:
continue
print('total %d to rename & converted %d jpgs' % (total_sum,i))

if __name__ == '__main__':
demo = BatchRename()
demo.rename()

参考博客:https://www.geek-share.com/detail/2723397382.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐