944,014 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4672
  • Python RSS
Jul 31st, 2006
0

Extract header info from image file

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lostpenan is offline Offline
3 posts
since Jul 2006
Aug 1st, 2006
0

Re: Extract header info from image file

For image files I would stick with binary file operation, it can handle text too. Here is example:
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Aug 3rd, 2006
0

Re: Extract header info from image file

Thanks! That's exactly what I need!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lostpenan is offline Offline
3 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Search File for Text and Copy
Next Thread in Python Forum Timeline: word unscrambler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC