Current numbering system, where just the actual number is appended to the end of the file name is very bad. Machines usually sort the filenames alphabetically which causes the images to be out of order when processing them programmatically!
This is default behavior for functions such as ls on UNIX systems or fsPromises.readdir(dirName) in Node.js, and produces the following output:
To fix this issue you have to zero pad the numbers: e.g.
'1' -> '001',
'27 -> '027',
'103' -> '103',
...
To get around this issue, I currently have to rename all the images afterwards, by first removing the .png extension, then splitting them by '_', then padding the numbers, and finally joining them back together.
This is the function I use to add zeros to the beginning of numbers:
/**
* Pads the beginning of a number, with the appropriate amount of zeros to equal the final desired length.
* @param { number } num the number we wish to pad
* @param { number } places the total padded length of the string
* @returns { string } zero padded string e.g. (12, 3) => '012'
*/
export const zeroPad = (num, places) => String(num).padStart(places, '0');
Padding by 3 places, allows for up to (& including) 999 ordered images.
Perhaps an additional option could be exposed, to choose the padding length (with default: 3).
Current numbering system, where just the actual number is appended to the end of the file name is very bad. Machines usually sort the filenames alphabetically which causes the images to be out of order when processing them programmatically!
This is default behavior for functions such as
ls
on UNIX systems orfsPromises.readdir(dirName)
in Node.js, and produces the following output:To fix this issue you have to zero pad the numbers: e.g. '1' -> '001', '27 -> '027',
'103' -> '103', ...
To get around this issue, I currently have to rename all the images afterwards, by first removing the
.png
extension, then splitting them by '_', then padding the numbers, and finally joining them back together.This is the function I use to add zeros to the beginning of numbers:
Padding by 3 places, allows for up to (& including) 999 ordered images.
Perhaps an additional option could be exposed, to choose the padding length (with default: 3).