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

python批量处理图片命名问题

2016-12-30 20:35 211 查看
(1)

将类似



的图片处理成从agricultural0开始命名

# -*- coding:utf8 -*-

import os

class BatchRename():

def __init__(self):
self.path = 'J:\Paper\Airport data sets' #要处理文件路径

def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 0
for item in filelist:
if item.endswith('.tif'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path),'agricultural'+str(i) + '.tif') #如果不需要图片命名不需要agricultural,则改为str(i) + '.tif

try:
os.rename(src, dst)
print 'converting %s to %s ...' % (src, dst)
i = i + 1
except:
continue
print 'total %d to rename & converted %d jpgs' % (total_num, i)

if __name__ == '__main__':

demo = BatchRename()
demo.rename()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息