Create Proxies for an Image Sequence

Problem

You have around 200 frames of Patches playing with his ball, and you wish to combine them into a QuickTime movie. The individual frames are named using the format Patches_ball_###.jpg, where ### is a three digit frame number in the range 001 to 200. Some of the frames are missing. You’d still like to create the movie, but you’d like proxies to be placed where the missing frames are.

Solution

import Draft
from DraftParamParser import ReplaceFilenameHashesWithNumber

proxy = Draft.Image.CreateImage( 640, 480 ) # Create a proxy frame.
proxy.SetToColor( Draft.ColorRGBA( 1, 0, 0, 1 ) )   # Make the proxy noticeable.

encoder = Draft.VideoEncoder( 'Patches.mov' )       # Initialize the video encoder.

# Note that the second parameter of range is one past the last frame number
for currFrame in range( 1, 201 ) :
    currFile = ReplaceFilenameHashesWithNumber( 'Patches_ball_###.jpg', currFrame )
    try:
        frame = Draft.Image.ReadFromFile( currFile )        # Try to read the frame.
    except:
        frame = proxy       # Reading was unsuccessful, use proxy frame.
    encoder.EncodeNextFrame( frame )        # Add the frame to the video.

encoder.FinalizeEncoding()  # Finalize and save the resulting video.

Discussion

If we try to read an image from a file that doesn’t exist, the ReadFromFile() method will throw an exception. Since we want to continue even if some of the images are missing, we use the try/except construct: we try to load in the image; if loading fails, we catch the exception (except) and use the proxy frame instead. In both cases we have a frame to add to the video, and so this statement comes after the try/except block.

Since the proxy frame is the same for all missing frames, we can create it once, before the loop. In this example, we have set the color of the proxy frame to red, so that it is easily located in the movie.

An alternate method is to replace the try/except construct with an if/else statement and the exists() function in the os.path module:

if os.path.exists( currFile ):
    frame = Draft.Image.ReadFromFile( currFile )    # Read the frame.
else:
    frame = proxy     # Reading was unsuccessful, use proxy frame.

See Also

Create A QuickTime Movie in the Basic Cookbook.