I need to rename 992 image names in the folder with Python. The name of the images should change based on their order. For example
old: image_1 new: P1_ES_1
old: image_2 new: P1_ES_2
old: image_3 new: P1_ES_3
old: image_4 new: P1_ED_1
old: image_5 new: P1_ED_2
old: image_6 new: P1_ED_3
old: image_7 new: P2_ES_1
old: image_8 new: P2_ES_2
old: image_9 new: P2_ES_3
old: image_10 new: P2_ED_1
...
this is the snippet with minor changes with me provided by @anki, but the problem is new name starts with ED, but it should be ES. any help will appreciated.
import os
import glob
path = 'F:/my_data/imagesResized/'
#path = 'F:/my_data/labelsResized/'
fns = glob.glob(path + '*.png')
fns.sort(key = len)
print(fns)
es_or_ed = 'ES'
for i, fn in enumerate(fns):
# Check for ED or ES
if i % 3 == 0 and es_or_ed == 'ES':
es_or_ed = 'ED'
elif i % 3 == 0 and es_or_ed == 'ED':
es_or_ed = 'ES'
# Create new filename
new_fn = 'P{}_{}_{}'.format(i // 6 + 1, es_or_ed, i%3+1)
#new_fn = 'P{}_{}_{}_{}'.format(i // 6 + 1, es_or_ed, i%3+1,"label")
# rename...S
os.rename(fn, os.path.join(path, new_fn + '.png'))