| | |
Extract header info from image file
Thread Solved |
•
•
Join Date: Jul 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
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.
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.
For image files I would stick with binary file operation, it can handle text too. Here is example:
Python Syntax (Toggle Plain Text)
# typical image header str1 = \ """BANDS = 1 BAND_STORAGE_TYPE = BAND_SEQUENTIAL BAND_NAME = "N/A" LINES = 2240 LINE_SAMPLES = 3840 SAMPLE_TYPE = UNSIGNED_INTEGER SAMPLE_BITS = 8 END ~~now starts binary image data~~ """ filename = "mc03.img" # write test image file fout = open(filename, "wb") fout.write(str1) fout.close() # read image file line by line for line in open(filename, "rb"): if 'END' in line: break if 'LINES' in line: # remove trailing newline line = line.rstrip('\n') # extract integer value print int(line.split('=')[1]) # 2240
![]() |
Similar Threads
- CD Image File (IT Professionals' Lounge)
- How to load a JPEG image file, store it in array and then save it (Visual Basic 4 / 5 / 6)
- how to display an attached image file in email (ASP)
- Python and the JPEG Image File, Part 2, The Image (Python)
- Python and the JPEG Image File, Part 1, The Header (Python)
- HELP! Html image file! (HTML and CSS)
- hi, how to set a proper path of a image file, many thanks! (C#)
Other Threads in the Python Forum
- Previous Thread: Search File for Text and Copy
- Next Thread: word unscrambler
| Thread Tools | Search this Thread |
address aliased anydbm app beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary dynamic events excel external feet file float format function generator getvalue handling homework iframe images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse number numbers output parsing path port prime programming py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile signal singleton slicenotation split string strings tails text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web whileloop word wxpython xlwt






