471,330 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

16 bit unsigned little endian reading issue

Hi everyone,

I have been working with Python since this summer and I used to frequent this board for advice and tips. Well...I'm back!

Anywho, I am reading in a file that is around 70 megs. It's a .img extension that contains quite a few CT slides. The way that a java based imaging program (ImageJ) reads it is a little unknown to me, but I know there is a header and the pictures follow.

My question is that when I open the file and read it to a variable, like:
data=open(filename,'r').read()

The variable (I believe is a unicode string) returns some data like this:
x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ etc.

How do I decode this so that I can use the info? As mentioned earlier I know it is in 16 bit unsigned little endian form. I have been reading some unicode stuff but have been coming up empty.

Thank you very much!
John Perry
Sep 24 '07 #1
1 3387
bartonc
6,596 Expert 4TB
Actually, you want to open in binary mode:
Expand|Select|Wrap|Line Numbers
  1. f = open('fName', 'rb')
  2. # then bite of a chunk of a known size
  3. # ie
  4. data = '\x00\x01\x00\x02\x00\x03\x00\x04'
  5. # then unpack it
  6. import struct
  7. print struct.unpack('HHHH', data)
  8. (256, 512, 768, 1024)
  9. print struct.unpack('>HHHH', data)
  10. (1, 2, 3, 4)
Sep 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by hicham | last post: by
19 posts views Thread by Vincent | last post: by
3 posts views Thread by nightolo | last post: by
18 posts views Thread by No Such Luck | last post: by
36 posts views Thread by Digital Puer | last post: by
14 posts views Thread by moumita | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.