Esempio n. 1
0
def movieRead(filename, asNumpy=True, **kwargs):
    """ movieRead(filename, asNumpy=True)
    
    Read the movie from GIF, SWF, AVI (or MPG), or a series of images (PNG,
    JPG,TIF,BMP). 
    
    Parameters
    ----------
    filename : string
        The name of the file that contains the movie. For a series of images,
        the `*` wildcard can be used.
    asNumpy : bool
        If True, returns a list of numpy arrays. Otherwise return 
        a list if PIL images.
    
    Notes
    ------
    Reading AVI requires the "ffmpeg" application:
      * Most linux users can install it using their package manager
      * There is a windows installer on the visvis website
    
    """
    
    # Get extension
    EXT = os.path.splitext(filename)[1]
    EXT = EXT[1:].upper()
    
    # Start timer
    t0 = time.time()
    
    # Write
    if EXT == 'GIF':
        images = readGif(filename, asNumpy, **kwargs)
    elif EXT == 'SWF':
        images = readSwf(filename,  asNumpy, **kwargs)
    elif EXT in videoTypes:
        images = readAvi(filename,  asNumpy, **kwargs)
    elif EXT in imageTypes:
        images = readIms(filename,  asNumpy, **kwargs)
    else:
        raise ValueError('Given file extension not valid: '+EXT)
    
    # Stop timer
    t1 = time.time()
    dt = t1-t0
    
    # Notify 
    if images:
        print "Read %i frames from %s in %1.2f seconds (%1.0f ms/frame)" % (
                        len(images), EXT, dt, 1000*dt/len(images))
    else:
        print "Could not read any images."
    
    # Done
    return images
Esempio n. 2
0
def movieRead(filename, asNumpy=True, **kwargs):
    """ movieRead(filename, asNumpy=True)
    
    Read the movie from GIF, SWF, AVI (or MPG), or a series of images (PNG,
    JPG,TIF,BMP). 
    
    Parameters
    ----------
    filename : string
        The name of the file that contains the movie. For a series of images,
        the `*` wildcard can be used.
    asNumpy : bool
        If True, returns a list of numpy arrays. Otherwise return 
        a list if PIL images.
    
    Notes
    ------
    Reading AVI requires the "ffmpeg" application:
      * Most linux users can install it using their package manager
      * There is a windows installer on the visvis website
    
    """

    # Get extension
    EXT = os.path.splitext(filename)[1]
    EXT = EXT[1:].upper()

    # Start timer
    t0 = time.time()

    # Write
    if EXT == 'GIF':
        images = readGif(filename, asNumpy, **kwargs)
    elif EXT == 'SWF':
        images = readSwf(filename, asNumpy, **kwargs)
    elif EXT in videoTypes:
        images = readAvi(filename, asNumpy, **kwargs)
    elif EXT in imageTypes:
        images = readIms(filename, asNumpy, **kwargs)
    else:
        raise ValueError('Given file extension not valid: ' + EXT)

    # Stop timer
    t1 = time.time()
    dt = t1 - t0

    # Notify
    if images:
        print "Read %i frames from %s in %1.2f seconds (%1.0f ms/frame)" % (
            len(images), EXT, dt, 1000 * dt / len(images))
    else:
        print "Could not read any images."

    # Done
    return images
Esempio n. 3
0
def readAvi(filename, asNumpy=True):
    """ readAvi(filename, asNumpy=True)
    
    Read images from an AVI (or MPG) movie.
    
    Requires the "ffmpeg" application:
      * Most linux users can install using their package manager
      * There is a windows installer on the visvis website
    
    """

    # Check whether it exists
    if not os.path.isfile(filename):
        raise IOError('File not found: ' + str(filename))

    # Determine temp dir, make sure it exists
    tempDir = os.path.join(os.path.expanduser('~'), '.tempIms')
    if not os.path.isdir(tempDir):
        os.makedirs(tempDir)

    # Copy movie there
    shutil.copy(filename, os.path.join(tempDir, 'input.avi'))

    # Run ffmpeg
    command = "ffmpeg -i input.avi im%d.jpg"
    S = subprocess.Popen(command,
                         shell=True,
                         cwd=tempDir,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    # Show what mencodec has to say
    outPut = S.stdout.read()

    if S.wait():
        # An error occured, show
        print outPut
        print S.stderr.read()
        # Clean up
        _cleanDir(tempDir)
        raise RuntimeError("Could not read avi.")
    else:
        # Read images
        images = images2ims.readIms(os.path.join(tempDir, 'im*.jpg'), asNumpy)
        # Clean up
        _cleanDir(tempDir)

    # Done
    return images
Esempio n. 4
0
def readAvi(filename, asNumpy=True):
    """ readAvi(filename, asNumpy=True)
    
    Read images from an AVI (or MPG) movie.
    
    Requires the "ffmpeg" application:
      * Most linux users can install using their package manager
      * There is a windows installer on the visvis website
    
    """

    # Check whether it exists
    if not os.path.isfile(filename):
        raise IOError("File not found: " + str(filename))

    # Determine temp dir, make sure it exists
    tempDir = os.path.join(os.path.expanduser("~"), ".tempIms")
    if not os.path.isdir(tempDir):
        os.makedirs(tempDir)

    # Copy movie there
    shutil.copy(filename, os.path.join(tempDir, "input.avi"))

    # Run ffmpeg
    command = "ffmpeg -i input.avi im%d.jpg"
    S = subprocess.Popen(command, shell=True, cwd=tempDir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    # Show what mencodec has to say
    outPut = S.stdout.read()

    if S.wait():
        # An error occured, show
        print outPut
        print S.stderr.read()
        # Clean up
        _cleanDir(tempDir)
        raise RuntimeError("Could not read avi.")
    else:
        # Read images
        images = images2ims.readIms(os.path.join(tempDir, "im*.jpg"), asNumpy)
        # Clean up
        _cleanDir(tempDir)

    # Done
    return images