1

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'))

enter image description here

AI_NA
  • 336
  • 2
  • 5
  • 20

1 Answers1

1

The reason why it starts with ES currently is zero-indexing. When i==0 during the first loop iteration, your code changes the value of es_or_ed to ED.

I revised your code to account for this, and also to perform a correct sort of your original filenames, as it appears that you do not have leading zeros in filenames and you want 10 to come after 9, not after 1. There is a function that I borrowed from this answer that will sort your list of filenames correctly.

import os
import glob

import re

def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
    return [int(text) if text.isdigit() else text.lower()
            for text in _nsre.split(s)]   

path = 'F:/my_data/labelsResized/'

fns = glob.glob(path + '*.png')

es_or_ed = 'ED'

for i, fn in enumerate(sorted(fns, key=natural_sort_key)):

    # Check for ED or ES

    if (i+1) % 3 == 1 and es_or_ed == 'ED':
        es_or_ed = 'ES'

    elif (i+1) % 3 == 1 and es_or_ed == 'ES':
        es_or_ed = 'ED'

    # Create new filename
    new_fn = 'P{}_{}_{}'.format(i // 6 + 1, es_or_ed, i%3+1) 

    # rename...S
    os.rename(fn, os.path.join(path, new_fn + '.png'))

Result (from code (not shown) where original filename is appended to new filename):

Screenshot

AlexK
  • 2,855
  • 9
  • 16
  • 27
  • thank you for sharing this. It's giving me the same problem and first it producing ED but I need ES. Also, I did change es_or_ed = 'ES' at the beginning but still same issue. – AI_NA Mar 07 '19 at 23:48
  • @Neda, how do you know that you are getting ED instead of ES? Is it because of how the files are sorted by name when you view them in your folder? Because if so, that's because "D" comes before "S" in the alphabet. I ran this code with an addition of the original filename to the new filename. You can see in the screenshot that I just added to the answer that the relationship you are looking for is there. – AlexK Mar 07 '19 at 23:54
  • @Neda, I intentionally changed the initial value of es_or_ed (before the loop) to "ED". – AlexK Mar 07 '19 at 23:56
  • I added image_1 which is original image and the rename one produce by your script which is P1_ED_1 and they are not identical. If I am doing wrong it's not possible to find out that image by browsing between 992 images. any idea? I expected image_1 change to the P1_ES_1 and both images be identical – AI_NA Mar 08 '19 at 00:05
  • @Neda, why are you comparing to P1_ED_1? image_1 should be the same as P1_ES_1, no? – AlexK Mar 08 '19 at 00:06
  • no it's not like that. I am comparing images from two different folder, original images and the rename one. There is an example of old name and new name in the question. – AI_NA Mar 08 '19 at 00:27
  • 1
    @Neda, the renamed file my script produces for image_1 is P1_ES_1, is that not clear? Please compare image_1 to P1_ES_1. – AlexK Mar 08 '19 at 00:29
  • Ohhhh, you are completely right. Thanks a lot @AlexK – AI_NA Mar 08 '19 at 00:36