I have tens of thousands of images that need to be shuffled, reordered, and renamed. Let’s implement this using Python.
First, batch shuffle the files in the folder.
Second, reorder the files.
Third, batch rename them.
import os, random, time
defrename(): # Configuration # File extension filetype = '.png' # File name prefix filename = '' # Number of digits in the filename, padding zeros in the beginning if less zfillNum = 5 # Configure the folder path here path = r"C:\Users\xxxx\Desktop\test" i = 0 filelist = os.listdir(path) # All files in the folder (including subfolders)
# Shuffle the order first random.shuffle(filelist) for files in filelist: # Iterate through all files Olddir = os.path.join(path, files) # Original file path if os.path.isdir(Olddir): # Skip if it's a folder continue filenameRes = str(int(time.time()) + random.randint(1000000, 88888888)) Newdir = os.path.join(path, filenameRes + filetype) # New file path os.rename(Olddir, Newdir) # Rename
filelist = os.listdir(path) # All files in the folder (including subfolders) for files in filelist: # Iterate through all files i = i + 1 Olddir = os.path.join(path, files) # Original file path if os.path.isdir(Olddir): # Skip if it's a folder continue filenameNum = str(i).zfill(zfillNum) filenameRes = filename + filenameNum Newdir = os.path.join(path, filenameRes + filetype) # New file path os.rename(Olddir, Newdir) # Rename returnTrue