Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Convert to Integer in Python

Name: Anonymous 2008-06-10 14:03


def readBMP( filename):
    f = file( filename)
    content = f.read()
    f.close()
    return content
   
def getDimensions( bytes):
    return bytes[18:22],bytes[22:26]

w,h = getDimensions( getBMP( 'test.bmp'))


how do i convert w and h to int?
atm (w,h) == ('\x05\x00\x00\x00', '\x05\x00\x00\x00')

Name: Anonymous 2008-06-10 14:54

The struct module will help (http://docs.python.org/lib/module-struct.html)

So your program would become:


from struct import unpack

def readBMP(filename):
    f = file(filename)
    content = f.read()
    f.close()
    return content
  
def getDimensions( bytes):
    return unpack('L',bytes[18:22]), unpack('L',bytes[22:26])

w,h = getDimensions(getBMP('test.bmp'))



Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List