473,883 Members | 1,763 Online

15 New Member
Hey Guys.
I'm looking for some info on how to read binary image data. Basicly RAW image files are strings of bits with say 2 bytes per pixel. What i need to do is read the value of each pixel in turn and store then in another variable, px1, pix2 etc. I'm running an iterative algorithm on 2 pixels at a time then moving to the next two pixels.
I am having alot of problems however reading the data. from the net and various books i see the readline method however this looks for new line charicters. I need something that will read the value of a certain number of bits from the file.

for example to read the first pixel data which is 16bits long i need something that will just read those 16bits and return its value.
Thanks.
Ed
Jun 26 '07
11 17670
eddiefisher41
15 New Member
Hey Guys. I managed to do it, thats for your help.
Here is what i came up with:
Expand|Select|Wrap|Line Numbers
1. def GetPxValue(openfile, bitdepth): #can iterate this part to get the next pixel
4.     if (bitdepth == 8):
5.         unpack_code = 'B'
6.     if (bitdepth == 16):
7.         unpack_code = 'BB'
8.     if (bitdepth == 32):
9.         unpack_code = 'BBBB'
10.     hex_value = struct.unpack(unpack_code, px_value)
11.     msbyte = Dec2Bin(hex_value[0])
12.     if (bitdepth == 16):
13.         lsbyte = Dec2Bin(hex_value[1])
14.         for i in range(8):
15.             msbyte.append(lsbyte[i])
16.     if (bitdepth == 32):
17.         byte2 = Dec2Bin(hex_value[1])
18.         byte3 = Dec2Bin(hex_value[2])
19.         lsbyte = Dec2Bin(hex_value[3])
20.         for i in range(8):
21.             msbyte.append(byte2[i])
22.         for i in range(8):
23.             msbyte.append(byte3[i])
24.         for i in range(8):
25.             msbyte.append(lsbyte[i])
26.     return msbyte
27. #return the whole bitdepth long pixel value as a list ready for filtering
28.
Note: Dec2Bin is a decimal to binary converter that returns a binary value as a list. the list is necessary for filtering in the next stage.
Cheers.
Ed
Jun 27 '07 #11
bartonc
6,596 Recognized Expert Expert
Hey Guys. I managed to do it, thats for your help.
Here is what i came up with:
Expand|Select|Wrap|Line Numbers
1. def GetPxValue(openfile, bitdepth): #can iterate this part to get the next pixel
4.     if (bitdepth == 8):
5.         unpack_code = 'B'
6.     if (bitdepth == 16):
7.         unpack_code = 'BB'
8.     if (bitdepth == 32):
9.         unpack_code = 'BBBB'
10.     hex_value = struct.unpack(unpack_code, px_value)
11.     msbyte = Dec2Bin(hex_value[0])
12.     if (bitdepth == 16):
13.         lsbyte = Dec2Bin(hex_value[1])
14.         for i in range(8):
15.             msbyte.append(lsbyte[i])
16.     if (bitdepth == 32):
17.         byte2 = Dec2Bin(hex_value[1])
18.         byte3 = Dec2Bin(hex_value[2])
19.         lsbyte = Dec2Bin(hex_value[3])
20.         for i in range(8):
21.             msbyte.append(byte2[i])
22.         for i in range(8):
23.             msbyte.append(byte3[i])
24.         for i in range(8):
25.             msbyte.append(lsbyte[i])
26.     return msbyte
27. #return the whole bitdepth long pixel value as a list ready for filtering
28.
Note: Dec2Bin is a decimal to binary converter that returns a binary value as a list. the list is necessary for filtering in the next stage.
Cheers.
Ed
Hi Ed. It's great when members post the solutions that they have come up with. I've added CODE tags to your post. Instructions on how to do this are on the right hand side of the page while you are posting or replying.

I felt sure that you would go with the array thing when I discovered it. I think that you will get a performance boost be reading larger chunks and working on slices of the array. The cool thing about python is that you may use one approach to get things working, then, with a little tweaking, make it work really well.

Have fun,
Barton
Jun 27 '07 #12