Modify picture names in batches

1. Self-contained

At this time, it is suitable to organize the folder names as a whole, but it is not good for those who have fixed requirements for writing programs.

Operation: right, rename, write image, it will be like the picture above, but all are (1) (2). . . There is a space followed by parentheses. This space will affect many programming and cannot be recognized unless the program is changed.

Two, use python yyds

import os.path

def rename(img_folder,num):
    for img_name in os.listdir(img_folder):  # os.listdir(): 列出路径下所有的文件
        #os.path.join() 拼接文件路径
        src = os.path.join(img_folder, img_name)   #src:要修改的目录名
        # dst = os.path.join(img_folder, img_name.split('t')[0]+'rot'+str(num) + '.jpg') #dst: 修改后的目录名      注意此处str(num)将num转化为字符串,继而拼接
        dst = os.path.join(img_folder, 'rot'+str(num) + '.jpg') #dst: 修改后的目录名      注意此处str(num)将num转化为字符串,继而拼接

        num= num+1
        os.rename(src, dst) #用dst替代src


def main():
    img_folder0 = 'D:/imagess' #直接放你的文件夹路径即可
    num=1
    rename(img_folder0,num)

if __name__=="__main__":
    main()
    print("改名成功")

The change was successful:

  

After the change, it’s cool, you can change it as you want 

3. Arbitrary sorting

What should I do if the original folder already has 900 images and I want to add another 100? Change the code

  

Python batch change picture name_rename photo with python

Guess you like

Origin blog.csdn.net/m0_63172128/article/details/129382801