发布时间:2022-10-01 17:30
【作用】将以相同后缀(.PNG,.JPG,…)的图片文件改成有序图。
【代码】
使用方法:只需要更改selfpath路径,改成需要变动的文件夹名称。
# -*- coding:utf-8 -*-
import os
class ImageRename():
def __init__(self):
#这句代码没有作用
self.path = 'G:\Data\times_square'
def rename(self):
# F:\PotPlayer\PotPlayerInstall\PotPlayer\Capture
selfpath='F:\\PotPlayer\\PotPlayerInstall\\PotPlayer\\Capture'
filelist = os.listdir(selfpath)
total_num = len(filelist)
i = 0
for item in filelist:
print(item)
if item.endswith('.jpg'):
src = os.path.join(os.path.abspath(selfpath), item)
dst = os.path.join(os.path.abspath(selfpath), 'image' + str(i) + '.jpg')
os.rename(src, dst)
print('converting %s to %s ...' % (src, dst))
i = i + 1
# print 'total %d to rename & converted %d jpgs' % (total_num, i)
if __name__ == '__main__':
newname = ImageRename()
newname.rename()