Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Graphics > Silicon Graphics > Python .mv -> i...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 306 of 352
Post > Topic >>

Python .mv -> images script

by jlstark@[EMAIL PROTECTED] May 5, 2006 at 11:39 AM

Hi,

   Just thought I'd drop this script on you all.  It converts .mv files
that are motion jpegs to <some other image format> suitable for a
quick:

ffmpeg -i frames_%04d.jpeg -o a.mpeg

   I just wrote this up this week and any feedback would be greatly
appreciated.  You will need to have PIL installed:

http://www.pythonware.com/products/pil/

Kind Regards,
Jeremy

====== sgi2foo.py ======

''' File:      sgi2foo.py
    Purpose:   Script for converting SGI .mv motion jpegs to something
else
    Author:    Jeremy Stark, jeremy@[EMAIL PROTECTED]
    Copyright: 2006 Jeremy Stark, Licensed under the GPL version 2 or
later
'''
im****t sys, os, types, getopt
im****t Image
from cStringIO im****t StringIO

def sgiExtract( file                 # Path string to the sgi movie
file
              , dest_dir             # Path to dump the images to
              , dest_name = 'frame_' # Base File name to use for images
              , extension = '.jpeg'  # Extension type (.jpeg or .png is
good)
              , size      = None     # Tuple (Width, Height) to scale
images to
              , optimized = True     # Use image codec optimizations
              , quality   = 99):     # JPEG quality setting
    ''' Decomposes an SGI format movie file to seperate image files
    '''
    header = sgiHeaderInfo(file)
    fptr = open(file, mode='r')
    data = fptr.read()
    fptr.close()
    images = data.split('\xFF\xD8\xFF\xE0')
    outfile = dest_dir + os.sep + dest_name + '%s' + extension

    if header['INTERLACING'] != '2':
        images =[Image.open(StringIO('\xFF\xD8\xFF\xE0'))+i for i in
images[1:]]
        return _saveImages(images, outfile, size, quality, optimized)

    delaced = []
    for i in range(1, len(images), 2):
        tmp = deinterlace(
Image.open(StringIO('\xFF\xD8\xFF\xE0'+images[i]))
                         ,
Image.open(StringIO('\xFF\xD8\xFF\xE0'+images[i+1])))
        delaced.append(tmp)
    return _saveImages(delaced, outfile, size, quality, optimized)

def _saveImages(images, outfile, size=None, quality=99,
optimized=True):
    ''' Helper method to save off the images
    '''
    c = 1
    pad = len(str(len(images)))
    for i in images:
        if size:
            i = i.resize(size)

i.save(outfile%(str(c).zfill(pad),),quality=quality,optimized=optimized)
        c += 1
    return len(images)

def deinterlace(i1, i2):
    image = ''
    bbox = i1.getbbox()
    line_width = bbox[2] * 3 # R,G,B pixel values = 3x line width
    s1 = i1.tostring()
    s2 = i2.tostring()
    for c in range(0, len(s1), line_width):
        image += s2[c:c+line_width]
        image += s1[c:c+line_width]
    return Image.fromstring('RGB', (bbox[2], bbox[3]*2), image)

def sgiPreviewFrame(file, frame=1):
    ''' Return a single frame
    '''
    fptr = open(file, mode='r')
    data = fptr.read()
    fptr.close()
    header = sgiHeaderInfo(file)
    images = data.split('\xFF\xD8\xFF\xE0')
    if header['INTERLACING'] == '2':
        return
deinterlace(Image.open(StringIO('\xFF\xD8\xFF\xE0'+images[frame]))

,Image.open(StringIO('\xFF\xD8\xFF\xE0'+images[frame+1])))
    return Image.open(StringIO('\xFF\xD8\xFF\xE0'+images[frame]))

def sgiHeaderInfo(file):
    ''' Return a dictionary of header info extracted from <file>
    '''
    fields = { '__NUM_I_TRACKS' : None
             , '__NUM_A_TRACKS' : None
             , 'LOOP_MODE'      : None
             , 'OPTIMIZED'      : None
             , 'NUM_LOOPS'      : None
             , 'WIDTH'          : None
             , 'COMPRESSION'    : None
             , 'ORIENTATION'    : None
             , 'Q_TEM****AL'     : None
             , 'HEIGHT'         : None
             , '__DIR_COUNT'    : None
             , 'INTERLACING'    : None
             , 'FPS'            : None
             , 'Q_SPATIAL'      : None
             , 'PACKING'        : None
             }
    file = open(file, mode='r')
    data = file.read(500)
    file.close()

    for field in fields.keys():
        start = data.find(field) + 20
        length = ord(data[data.find(field) + 19])
        fields[field] = data[start:start+length-1]
    return fields

def usage():
    print '''
sgi2foo.py : Copyright 2006, Jeremy Stark, Released under the GPL

Usage:

python sgi2foo.py -i themovie.mv -o /out/dir -O -q 99 -w 720 -h 496

-i   : The input movie
-o   : The output directory to dump all the images to
-q   : JPEG quality setting
-O   : Optimize flag (tells the codec to optimize images, whatever that
means)
-w   : Force output width, ignore if you want the original size
-h   : Force output height.  Required if you specified -w
'''

def main():
    opts, pargs = getopt.getopt(sys.argv[1:], 'Oi:o:q:h:w:',[])
    optimized = False
    file = outfile = height = width = size = None
    quality = 99

    for opt in opts:
        if opt[0] == '-O':
            optimized = True
        if opt[0] == '-i':
            file = opt[1]
        if opt[0] == '-o':
            outdir = opt[1]
        if opt[0] == '-q':
            quality = opt[1]
        if opt[0] == '-h':
            height = opt[1]
        if opt[0] == '-w':
            width = opt[1]

    if height and width:
        size = (int(width), int(height))

    sgiExtract(file, outdir, quality=quality, optimized=optimized,
size=size)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        usage()
    else:
        main()

===== End script ====
 




 1 Posts in Topic:
Python .mv -> images script
jlstark@[EMAIL PROTECTED]  2006-05-05 11:39:44 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 21:02:43 CDT 2008.