I have an image file and I'd like to check if its part of an image sequence using python.
For example i start with this file:
/projects/image_0001.jpg
and i want to check if the file is part of a sequence i.e.
/projects/image_0001.jpg
/projects/image_0002.jpg
/projects/image_0003.jpg
...
Checking for whether there is a sequence of images seems simple if i can determine if the file name could be art of a sequence, i.e. if there is a sequence of numbers of the file name
My first though was to ask the user to add ####
to the file path where the numbers should be and input a start and end frame number to replace the hashes with but this is obviously not very user friendly. Is there a way to check for a sequence of numbers in a string with regular expressions or something similar?
It's relatively easy to use python's
re
module to see if a string contains a sequence of digits. You could do something like this:This will return a list of all digits sequences in
filename
. If:...then maybe they're part of a sequence.
I'm assuming the problem is more for being able to differentiate between sequenced files on disk than knowing any particular information about the filenames themselves.
If thats the case, and what you're looking for is something that is smart enough to take a list like:
And get back a result saying - I have 2 sequences of files: /path/to/file_#.png and /path/to/image_#.png you are going to need 2 passes - 1st pass to determine valid expressions for files, 2nd pass to figure out what all other files meet that requirement.
You'll also need to know if you're going to support gaps (is it required to be sequential)
Is this 1 sequence (/path/to/file_#.png) or 2 sequences (/path/to/file_1-3.png, /path/to/file_5-7.png)
Also - how do you want to handle numeric files in sequences?
etc.
With that in mind, this is how I would accomplish it:
And an example usage:
There is a method in there that refers to natural sorting, which is a separate topic. I just used my natural sort method from my projex library. It is open-source, so if you want to use or see it, its here: http://dev.projexsoftware.com/projects/projex
But that topic has been covered elsewhere on the forums, so Just used the method from the library.