Extract header info from image file

Thread Solved

Join Date: Jul 2006
Posts: 3
Reputation: lostpenan is an unknown quantity at this point 
Solved Threads: 0
lostpenan lostpenan is offline Offline
Newbie Poster

Extract header info from image file

 
0
  #1
Jul 31st, 2006
I would like to extract the header info from an image file. For example I would like to search for the key "LINES" and return "2240" as string. And should I use open("r") or open("b") since im only interested in the header.

Header:



BANDS = 1
BAND_STORAGE_TYPE = BAND_SEQUENTIAL
BAND_NAME = "N/A"
LINES = 2240
LINE_SAMPLES = 3840
SAMPLE_TYPE = UNSIGNED_INTEGER
SAMPLE_BITS = 8
END





I wonder if this is the right direction.

file = "D:\\IMG\\mc03.img"
f = open(file,"r").readline()

for line in f:
if line.find("BAND"):
s = line.split()
h_band = s[2]
if line.find("LINES"):
s = line.split()
h_lines = s[2]

Thanks for any suggestions.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Extract header info from image file

 
0
  #2
Aug 1st, 2006
For image files I would stick with binary file operation, it can handle text too. Here is example:
  1. # typical image header
  2. str1 = \
  3. """BANDS = 1
  4. BAND_STORAGE_TYPE = BAND_SEQUENTIAL
  5. BAND_NAME = "N/A"
  6. LINES = 2240
  7. LINE_SAMPLES = 3840
  8. SAMPLE_TYPE = UNSIGNED_INTEGER
  9. SAMPLE_BITS = 8
  10. END
  11. ~~now starts binary image data~~
  12. """
  13.  
  14. filename = "mc03.img"
  15.  
  16. # write test image file
  17. fout = open(filename, "wb")
  18. fout.write(str1)
  19. fout.close()
  20.  
  21. # read image file line by line
  22. for line in open(filename, "rb"):
  23. if 'END' in line:
  24. break
  25. if 'LINES' in line:
  26. # remove trailing newline
  27. line = line.rstrip('\n')
  28. # extract integer value
  29. print int(line.split('=')[1]) # 2240
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 3
Reputation: lostpenan is an unknown quantity at this point 
Solved Threads: 0
lostpenan lostpenan is offline Offline
Newbie Poster

Re: Extract header info from image file

 
0
  #3
Aug 3rd, 2006
Thanks! That's exactly what I need!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC