def write_t2b(t2bfile, coverdata=None):
 from PIL import Image
 if (coverdata is not None):
  coverdata = StringIO.StringIO(coverdata)
  cover = Image.open(coverdata).convert('L')
  cover.thumbnail((96, 144), Image.ANTIALIAS)
  t2bcover = Image.new('L', (96, 144), 'white')
  (x, y) = cover.size
  t2bcover.paste(cover, (((96 - x) / 2), ((144 - y) / 2)))
  px = []
  pxs = t2bcover.getdata()
  for i in range(len(pxs)):
   px.append(pxs[i])
   if (len(px) >= 4):
    binstr = (((i2b(reduce_color(px[0])) + i2b(reduce_color(px[1]))) + i2b(reduce_color(px[2]))) + i2b(reduce_color(px[3])))
    t2bfile.write(chr(int(binstr, 2)))
    px = []
 else:
  t2bfile.write(DEFAULT_T2B_DATA)

def write_t2b ( t2bfile , coverdata = None ) : from PIL import Image if ( coverdata is not None ) : coverdata = StringIO . StringIO ( coverdata ) cover = Image . open ( coverdata ) . convert ( 'L' ) cover . thumbnail ( ( 96 , 144 ) , Image . ANTIALIAS ) t2bcover = Image . new ( 'L' , ( 96 , 144 ) , 'white' ) ( x , y ) = cover . size t2bcover . paste ( cover , ( ( ( 96 - x ) / 2 ) , ( ( 144 - y ) / 2 ) ) ) px = [ ] pxs = t2bcover . getdata ( ) for i in range ( len ( pxs ) ) : px . append ( pxs [ i ] ) if ( len ( px ) >= 4 ) : binstr = ( ( ( i2b ( reduce_color ( px [ 0 ] ) ) + i2b ( reduce_color ( px [ 1 ] ) ) ) + i2b ( reduce_color ( px [ 2 ] ) ) ) + i2b ( reduce_color ( px [ 3 ] ) ) ) t2bfile . write ( chr ( int ( binstr , 2 ) ) ) px = [ ] else : t2bfile . write ( DEFAULT_T2B_DATA )
t2bfile is a file handle ready to write binary data to disk .