#!/bin/env python # # convert_yuv.py # # Converts a bunch of separate image files into animation sequences that can be used with # Nokia Series 60 emulator # # Dec 7, 2004 # kari.pulli@nokia.com import sys, os, time from glob import glob from array import * import Image from optparse import OptionParser sizes = { 'sqcif': (128,96), 'qcif' : (176,144), 'cif' : (352,288), 'qqvga': (160,120), 'qvga' : (320,240), 'vga' : (640,480), } def create_yuv( resolution, files, targetdir ): w,h = sizes[ resolution ] a = array('B') # array of unsigned bytes for f in files: print 'Processing', f, 'at size', w, h im = Image.open(f) im.load() # convert to correct size, and from rgb to yuv # use the ITU-R BT.601 conversion matrix but with full range RGB # this is the same as in JPEG (Jfif specs V1.02) encoding (CCIR 601-256 levels) rgb2yuv = [ 0.299, 0.587, 0.114, 0, -0.168736, -0.331264, 0.5, 128, 0.5, -0.418688, -0.081312, 128 ] data = im.resize( (w,h), Image.BICUBIC ).convert( 'RGB', rgb2yuv ).getdata() # save in YUV 422, Nokia's ordering for i in range(0, w*h, 2): Y1,U1,V1 = data[i] Y2,U2,V2 = data[i+1] a.append( Y2 ) a.append( int((V1+V2)*.5) ) a.append( Y1 ) a.append( int((U1+U2)*.5) ) name = os.path.join(targetdir, 'handshake_%s.yuv' % resolution) print 'Saving file', name a.tofile( file( name, 'wb' ) ) if __name__ == '__main__': usage = """usage: %prog [options] filebase Converts a bunch of separate image files into animation sequences that can be used with Nokia Series 60 emulator (at least 7610) If you copy these files into EPOCROOT/epoc32/wins/c, the emulator will use these files when emulating video camera (instead of using the default underwater sequence). filebase is the path and base file name, e.g., c:/tmp/blah if your files are in c:/tmp/ and the names are blah0001.jpg, etc.""" parser = OptionParser( usage = usage ) parser.add_option( '-t', '--targetdir', dest = 'targetdir', default = '.', help = 'target directory where to store the files (overwrites old ones)' ) (options, args) = parser.parse_args() if not args: parser.print_help() sys.exit() # read in the images files = glob( args[0] + '*' ) files.sort() start = time.clock() for resolution in sizes.keys(): create_yuv( resolution, files, options.targetdir ) print 'Took', time.clock() - start, 'seconds'