I have a folder which contains images. The name is like this :
ID03_013_bmp.rf.1d3821394d2c0b482202e204edde93b1.jpg
I want to rename each image, save 8 first characters and remove the rest. and thank you.
I have a folder which contains images. The name is like this :
ID03_013_bmp.rf.1d3821394d2c0b482202e204edde93b1.jpg
I want to rename each image, save 8 first characters and remove the rest. and thank you.
You can use the following script, which uses the os
standard library.
import os
folder_dir = "/your/folder/directory" # Path to your folder
filenames = os.listdir(folder_dir) # List all the files in the folder
ids = [] # List of the the first 8 characters of your filenames
for file in filenames:
id = file.split(".")[0][:8]
ids.append(id)
target_name = id + ".jpg" # Define how you want your files to be named, currently based on the first 8 characters
current_path = os.path.join(folder_dir, file)
target_path = os.path.join(folder_dir, target_name)
os.rename(current_path, target_path)
import os
# Save the folder location in a variable path
# Windows
# path = "C://Users//<username>//"
# macOS
path = "/Users/<username>/..."
# Get all filenames in that path into a list
images = os.listdir(path)
# Loop over the list
for i in images:
# Build new file name with first 8 letters of the old file name + suffix .jpg
new_name = f"{i[:8]}.jpg"
# display the new name
print(new_name)
# Do the rename operation (you can comment this out for testing)
os.rename(f"{path}{i}", f"{path}{new_name}")