def write_t4b(t4bfile, coverdata=None):
 from PIL import Image
 if (coverdata is not None):
  coverdata = BytesIO(coverdata)
  cover = Image.open(coverdata).convert(u'L')
  cover.thumbnail((96, 144), Image.ANTIALIAS)
  t4bcover = Image.new(u'L', (96, 144), u'white')
  (x, y) = cover.size
  t4bcover.paste(cover, (((96 - x) // 2), ((144 - y) // 2)))
  pxs = t4bcover.getdata()
  t4bfile.write('t4bp')
  data = (((16 * reduce_color(pxs[i])) + reduce_color(pxs[(i + 1)])) for i in xrange(0, len(pxs), 2))
  t4bfile.write(bytes(bytearray(data)))
 else:
  t4bfile.write(DEFAULT_T4B_DATA)

def write_t4b ( t4bfile , coverdata = None ) : from PIL import Image if ( coverdata is not None ) : coverdata = BytesIO ( coverdata ) cover = Image . open ( coverdata ) . convert ( u'L' ) cover . thumbnail ( ( 96 , 144 ) , Image . ANTIALIAS ) t4bcover = Image . new ( u'L' , ( 96 , 144 ) , u'white' ) ( x , y ) = cover . size t4bcover . paste ( cover , ( ( ( 96 - x ) // 2 ) , ( ( 144 - y ) // 2 ) ) ) pxs = t4bcover . getdata ( ) t4bfile . write ( 't4bp' ) data = ( ( ( 16 * reduce_color ( pxs [ i ] ) ) + reduce_color ( pxs [ ( i + 1 ) ] ) ) for i in xrange ( 0 , len ( pxs ) , 2 ) ) t4bfile . write ( bytes ( bytearray ( data ) ) ) else : t4bfile . write ( DEFAULT_T4B_DATA )
t4bfile is a file handle ready to write binary data to disk .