2021-03-07

python打乱文件夹下所有图片

import random
import os


Big_Image_path = r"D:\ML\images\leaf_enhance\leaf_image" # 大文件夹路径
foldres = os.listdir(Big_Image_path)
print(foldres)

for dirname in foldres:
    small_Image_path = Big_Image_path + '/' + dirname
    files = os.listdir(small_Image_path)  # 获取图片文件夹路径
    Imgnum = len(files)


    L = random.sample(range(0, Imgnum), Imgnum)
    print(L)

    filetype = ".jpg"  # 文件类型

    i = 0
    for filename in files:
        portion = os.path.splitext(filename)  # 将文件名拆成名字和后缀
        if portion[1] == filetype:  # 检查文件的后缀

            newname =  str(L[i]) + filetype
            print(newname)
            os.rename(small_Image_path + '/' + filename, small_Image_path + '/' + newname)  # 修改名称
            i = i + 1

你可能感兴趣的:(python)