Can read in the BMP data correctly ,but the size is not right?
Jimmie He
jimmie.he at gmail.com
Tue Apr 30 09:09:52 EDT 2013
On Tuesday, April 30, 2013 1:57:07 AM UTC+8, Peter Otten wrote:
> Jimmie He wrote:
>
>
>
> > I'm trying to read in the BMP data by the the code below,and I'm check
>
> > the data array with WINHEX,and it is correct,but which confuse me is
>
> > why the size is 0x180,but the actual picture should be 48*48 = 0x120
>
> > bytes because I use 1-bit BMP not the 24bit BMP,could any one give some
>
> > hints?
>
>
>
> According to wikipedia <http://en.wikipedia.org/wiki/BMP_file_format>
>
>
>
> """
>
> The size of each row is rounded up to a multiple of 4 bytes [...]
>
> """
>
>
>
> So 48/8 == 6 will be rounded to 8, and 8*48 == 384 == 0x180.
>
>
>
> > handle1=open( bmpfilename ,"rb")
>
> > raw = bytearray(handle1.read( ))
>
> > handle1.close
>
>
>
> To actually do something the last line should be handle1.close(). I
>
> recommend
>
>
>
> with open(bmpfilename ,"rb") as handle1:
>
> raw = bytearray(handle1.read())
>
>
>
> instead which has the additional advantage that the file will be closed if
>
> an exception occurs in the with-suite.
I've successfully read the correct data from BMP now by your advice,thanks again Peter and other helpful guy.Especially the BMP_file_format from wiki,very Visualize.
More information about the Python-list
mailing list