Create a Left-Eye, Right-Eye, Side-By-Side Movie


You have a set of frames pairs (left eye, right eye) for creating a 3D movie of Patches playing with his ball, and you want to use them to create a left-eye, right-eye, side-by-side movie, where each side is 640x480. The left eye images are saved using the filenames Patches_ball_left_###.jpg, with ### replaced by the three digit frame number, and the right eye images are similarly stored using the filenames Patches_ball_right_###.jpg.


import Draft
from DraftParamParser import ReplaceFilenameHashesWithNumber

width = 640
doubleWidth = width * 2  # twice the width to fit both left and right eye frames.
height = 480
encoder = Draft.VideoEncoder( '', width=doubleWidth )  # Create encoder.

for currFrame in range( 1, 201 ): # Note: second parameter of range is one past end
    nameL = ReplaceFilenameHashesWithNumber( 'Patches_ball_left_###.jpg', currFrame )
    nameR = ReplaceFilenameHashesWithNumber( 'Patches_ball_right_###.jpg', currFrame )

    frameL = Draft.Image.ReadFromFile( nameL )
    frameR = Draft.Image.ReadFromFile( nameR )

    frameL.Resize( width, height )  # Make sure frames are correct size.
    frameR.Resize( width, height )

    frame = Draft.Image.CreateImage( doubleWidth, height )  # Frame to hold both eyes.
    compOp = Draft.CompositeOperator.OverCompositeOp
    frame.CompositeWithAnchor( frameL, Draft.Anchor.West, compOp )
    frame.CompositeWithAnchor( frameR, Draft.Anchor.East, compOp )

    encoder.EncodeNextFrame( frame )        # Add the frame to the video.

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


The main difference between this recipe and the basic Create a QuickTime Movie recipe is that here we have two images per frame of the movie, and we must load both and composite them into a single image before we can add them to the video encoder.

Using Draft.Anchor.West and Draft.Anchor.East we can easily place the images without having to worry about finding the exact location in the final image to use for the Composite() call. The left frame is placed at the leftmost edge with the West anchor and the right frame is place at the rightmost edge using the East anchor.

See Also

Create a QuickTime Movie, Resize an Image, and Composite Two Images in the Basic Cookbook