A quick snippet for checking image sequences

Image sequences, love ’em. All those distributed renders and so on create hundreds of thousands of image files on my media drive, so it’s good to have a quick scrip for checking that a sequence has rendered completely without any gaps.

Here’s one in Powershell. It tells you if any files are not in sequence and reports on how many there are.

$i=0; #set to the first frame of your sequence
ls .\ |%{  
  while (! ( $_.name -match ("{0}{1}" -f $i, $_.extension))){
   write-host "missing frame $i" -ForegroundColor Red;
   $i += 1;
  };
  echo $i;
  $i+=1;
  [Console]::SetCursorPosition($Host.UI.RawUI.CursorPosition.X,  $Host.UI.RawUI.CursorPosition.Y-1)
}; echo "last frame $i"

If your sequence doesn’t start at zero it will report the missing frames up to the start of your sequence. You can change that behaviour by setting $i to the first frame.

If you add it to your powershell config file you can add a couple of bells and whistles, including the ability to choose a folder and first frame:

function Check-ImageSequence{
  Param ($searchFolder = ".\",
        [int]$StartFrame = 0)
  $i=$StartFrame;
  ls (get-item $searchFolder) |%{ 
    while (! ( $_.name -match ("{0}{1}" -f $i, $_.extension))){ 
      write-host "missing frame $i" -ForegroundColor Red; 
      $i += 1;
    };
    write-host "checking $i";
    $i+=1;
    #wind back the cursor position so it doesn't scroll
    [Console]::SetCursorPosition($Host.UI.RawUI.CursorPosition.X, $Host.UI.RawUI.CursorPosition.Y-1);
  };
  echo "last frame $i"
}

The code prints out each missing file and counts how many images there are in the sequence.

I use pngs a lot, and there’s a useful tool called pngcheck that makes sure that the png file is ok – sometimes if I kill a render node mid-save I end up with half-completed frames, which cause headaches when I try to import them in post.

You can add pngcheck to the mix thus:

function Check-ImageSequence{ 
  Param ($searchFolder = ".\",
        [int]$StartFrame = 0,
        [switch]$removeDodgyPngs
  )
  $i=$StartFrame;
  ls (get-item $searchFolder) |%{
    while (! ( $_.name -match ("{0}{1}" -f $i, $_.extension))){
      write-host "missing frame $i" -ForegroundColor Red; 
      $i += 1;
    };
    echo "checking $i";
    #wind back the cursor position so it doesn't scroll;
    [Console]::SetCursorPosition($Host.UI.RawUI.CursorPosition.X, $Host.UI.RawUI.CursorPosition.Y-1);
    if ($_.extension -eq ".png"){ #only check png files
      $badFile = ((pngcheck.exe $_.fullname 2>&1) -match "Error");
      if ($badFile){
        write-host "$_ is dodgy" -ForegroundColor Red;
        if ($removeDodgyPngs){ rm $_.fullname; }
      }
    }
    $i+=1;

  };
  echo "last frame $i";
}

Note that by default this script won’t remove any dodgy ones. If you specify -RemoveDodgyPngs then it will. That’s why it checks that the file actually is a png file, in case you accidentally run it on a folder full of jpegs.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.