Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44509 > unrolled thread
| Started by | Jimmie He <jimmie.he@gmail.com> |
|---|---|
| First post | 2013-04-29 10:20 -0700 |
| Last post | 2013-04-29 11:12 -0700 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.python
Can read in the BMP data correctly ,but the size is not right? Jimmie He <jimmie.he@gmail.com> - 2013-04-29 10:20 -0700
Re: Can read in the BMP data correctly ,but the size is not right? Chris Angelico <rosuav@gmail.com> - 2013-04-30 03:53 +1000
Re: Can read in the BMP data correctly ,but the size is not right? Peter Otten <__peter__@web.de> - 2013-04-29 19:57 +0200
Re: Can read in the BMP data correctly ,but the size is not right? Jimmie He <jimmie.he@gmail.com> - 2013-04-30 06:09 -0700
Re: Can read in the BMP data correctly ,but the size is not right? Jimmie He <jimmie.he@gmail.com> - 2013-05-04 03:26 -0700
Re: Can read in the BMP data correctly ,but the size is not right? MRAB <python@mrabarnett.plus.com> - 2013-04-29 19:10 +0100
Re: Can read in the BMP data correctly ,but the size is not right? 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-29 11:12 -0700
| From | Jimmie He <jimmie.he@gmail.com> |
|---|---|
| Date | 2013-04-29 10:20 -0700 |
| Subject | Can read in the BMP data correctly ,but the size is not right? |
| Message-ID | <aee818ca-e077-4e52-be55-7b8282684ba4@googlegroups.com> |
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?
--------------------------------------------------------------------------------
__Head_Info = [
[ 'Type' ,0 , 2],#BM
[ 'FSize' ,2 , 4],#File Size
[ 'Reserved' ,6 , 4],#0x00000000
[ 'OffBits' ,10 , 4],#Offset of Image
[ 'SSize' ,14 , 4],# 40
[ 'Width' ,18 , 4],#Width
[ 'Height' ,22 , 4],#Hight
[ 'Planes' ,26 , 2],#1
[ 'BitCount' ,28 , 2],#{1,2,4,8,24}
[ 'Compress' ,30 , 4],#0
[ 'SizeImage' ,34 , 4],#Bytes Per Line
[ 'XPM' ,38 , 4],#2835
[ 'YPM' ,42 , 4],#2835
[ 'ClrUsed' ,46 , 4],#0
[ 'ClrImportant' ,50 , 4]#0
]
_Type =0;
_FSize =1;
_Reserved =2;
_OffBits =3;
_SSize =4;
_Width =5;
_Height =6;
_Planes =7;
_BitCount =8;
_Compress =9;
_SizeImage =10;
_XPM =11;
_YPM =12;
_ClrUsed =13;
_ClrImportant =14;
def __getInt( b, idx):
return binToInt(b,__Head_Info[idx][1],__Head_Info[idx][2])
def saveMatrixtoASC(bmpfilename,ascfilename):
try:
handle1=open( bmpfilename ,"rb")
raw = bytearray(handle1.read( ))
handle1.close
except Exception as E:
return "error:"+ str(E),""
datastart=__getInt(raw, _OffBits)
datasize =__getInt(raw, _SizeImage)
print ('Image Offset = 0x%X'%datastart)
print ('Image Size = 0x%X'%datasize)
handle2=open( ascfilename ,"w")
for i in range(0,datasize):
handle2.write('0x%02X,'%raw[datastart+i])
if (i+1) % 16 == 0 :
handle2.write("\n")
handle2.close
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-04-30 03:53 +1000 |
| Message-ID | <mailman.1158.1367258024.3114.python-list@python.org> |
| In reply to | #44509 |
On Tue, Apr 30, 2013 at 3:20 AM, Jimmie He <jimmie.he@gmail.com> wrote: > handle1.close I haven't looked at the rest of the code, but be careful of this: You aren't actually *calling* this function. That might be your problem and it might not, but try fixing it (add the parentheses, even though there's nothing to put in them) and see if that helps. Same with the handle2.close at the end. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-04-29 19:57 +0200 |
| Message-ID | <mailman.1159.1367258242.3114.python-list@python.org> |
| In reply to | #44509 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Jimmie He <jimmie.he@gmail.com> |
|---|---|
| Date | 2013-04-30 06:09 -0700 |
| Message-ID | <6fb1e305-22d3-42df-ab08-9e99c91291f7@googlegroups.com> |
| In reply to | #44513 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Jimmie He <jimmie.he@gmail.com> |
|---|---|
| Date | 2013-05-04 03:26 -0700 |
| Message-ID | <0b3223e6-2414-45a3-9493-3df54ee643d6@googlegroups.com> |
| In reply to | #44513 |
Totally right!Thanks again!
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-04-29 19:10 +0100 |
| Message-ID | <mailman.1160.1367259060.3114.python-list@python.org> |
| In reply to | #44509 |
On 29/04/2013 18:20, 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? > [snip] What size is 0x180? If you're asking why the file size is 0x180 and not 0x120, it's simply because of the header. An image file contains not just the pixels of the image, but also information about the image.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2013-04-29 11:12 -0700 |
| Message-ID | <2d1c49ed-08b3-4a56-8287-4497e1fb545f@googlegroups.com> |
| In reply to | #44509 |
Jimmie He於 2013年4月30日星期二UTC+8上午1時20分49秒寫道:
> 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?
>
>
>
>
>
> --------------------------------------------------------------------------------
>
> __Head_Info = [
>
> [ 'Type' ,0 , 2],#BM
>
> [ 'FSize' ,2 , 4],#File Size
>
> [ 'Reserved' ,6 , 4],#0x00000000
>
> [ 'OffBits' ,10 , 4],#Offset of Image
>
> [ 'SSize' ,14 , 4],# 40
>
> [ 'Width' ,18 , 4],#Width
>
> [ 'Height' ,22 , 4],#Hight
>
> [ 'Planes' ,26 , 2],#1
>
> [ 'BitCount' ,28 , 2],#{1,2,4,8,24}
>
> [ 'Compress' ,30 , 4],#0
>
> [ 'SizeImage' ,34 , 4],#Bytes Per Line
>
> [ 'XPM' ,38 , 4],#2835
>
> [ 'YPM' ,42 , 4],#2835
>
> [ 'ClrUsed' ,46 , 4],#0
>
> [ 'ClrImportant' ,50 , 4]#0
>
> ]
>
> _Type =0;
>
> _FSize =1;
>
> _Reserved =2;
>
> _OffBits =3;
>
> _SSize =4;
>
> _Width =5;
>
> _Height =6;
>
> _Planes =7;
>
> _BitCount =8;
>
> _Compress =9;
>
> _SizeImage =10;
>
> _XPM =11;
>
> _YPM =12;
>
> _ClrUsed =13;
>
> _ClrImportant =14;
>
>
>
> def __getInt( b, idx):
>
> return binToInt(b,__Head_Info[idx][1],__Head_Info[idx][2])
>
>
>
> def saveMatrixtoASC(bmpfilename,ascfilename):
>
> try:
>
> handle1=open( bmpfilename ,"rb")
>
> raw = bytearray(handle1.read( ))
>
> handle1.close
>
> except Exception as E:
>
> return "error:"+ str(E),""
>
>
>
> datastart=__getInt(raw, _OffBits)
>
> datasize =__getInt(raw, _SizeImage)
>
> print ('Image Offset = 0x%X'%datastart)
>
> print ('Image Size = 0x%X'%datasize)
>
> handle2=open( ascfilename ,"w")
>
> for i in range(0,datasize):
>
> handle2.write('0x%02X,'%raw[datastart+i])
>
> if (i+1) % 16 == 0 :
>
> handle2.write("\n")
>
> handle2.close
The start of each line of bytes must be in the 32 bit=4byte
boundary in the MS BMP format.
Please read the MS specs.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web