How to change name of images to 1.jpg, 2.jpg,... automatically?

I need to rename several images to numerical like 1.jpg, 2.jpg … Any suggestion to do it (of course not by hand)?
for example if I can give a folder as input and and pictures names in that folder being changed to 1.jpg, 2.jpg …

in a for loop, you could iterate through a list of files and use the ‘rename()’ function to rename each image, then resave them as .jpg files.

you can convert your loop index to a string and rename each image with rename(toString(var)) where var is your loop index.

2 Likes

Hello @Zeynab_Mousavi ,

If you don’t need to do it as part of an image processing routine, I would highly recommend Bulk Rename Utility: https://www.bulkrenameutility.co.uk

I use it all the time and it has just about every option you might need.

As @Wilson_Adams suggests this is fairly trivial to to in IJ1 script using the templates found in the script editor (Look for one called process folders).

The simplest way I could think to do this is with the File.rename function (see here)

I’ve put together an example using the template and the File.rename function here (be warned this will rename your originals with no undo):
https://bitbucket.org/snippets/davemason/ean6RG

If you want to keep the originals you need to follow @Wilson_Adams’s suggestion and use save as instead so something more like this that takes both an input and output:

https://bitbucket.org/snippets/davemason/AeBXgG

Note that in both cases you can edit the zeroPad variable at the top to decide with how many zeros you wish to pad the number.

Hope that helps.

2 Likes

@Wilson_Adams @dnmason Thanks a lot will try your solution and let you know.

@dnmason I used the code you suggested but there are 3 zeros before the number so I get error when running images with e.g. 0001 name instead of only one. How can I get rid of zeroPad? I might have more than 9 images or more than 99 images but I dont want to have zeros before the number.

The easiest way is to change the zero padding variable at the top of the code from:

zeroPad=4;
to
zeroPad=0;

This will give you the format you require.

For anyone that is curious, providing either 1 or 0 to IJ.pad will give the same result.

2 Likes

@dnmason How about if images are more than 9?
I added if condition inside the loop to determine zeroPad based on list number.

@Zeynab_Mousavi, this is really very easy to test in a script. Just run the following lines and you’ll see that if IJ.pad is run with a two digit number and a zero as the padding parameter, it will return the two digit number.

print(IJ.pad(9,0));
print(IJ.pad(99,0));

Capture

Did you actually try running the script as it was?

if you´re on windows I can highly recommend FreeCommander, a file manager that includes besides many other useful features also a powerful bulk renaming tool.
https://freecommander.com/

1 Like

Thanks @dnmason. My question was that I had more than 9 figures so I needed to have 1 to 9 as well as 10 and so on both simultaneously. I know I can change it but the question was to how to apply that so it can work for both cases. The easiest way I found was if condition.

Yes, and my reply (I thought) clarified that as long as you set the zeroPad variable to zero as instructed, it would correctly name your files 1.jpg, 2.jpg99.jpg, 100.jpg for as many images as you have.

I had hoped that you would have found this had you run the script. You shouldn’t need an IF loop.

1 Like

@dnmason Worked. Thanks for clarification.

1 Like