[Tutor] Parsing os.popen(command) output
Bill Burns
billburns at pennswoods.net
Sun Sep 11 21:04:32 CEST 2005
[Bill]
>>Here's the typical output from the command:
>>[start output]
>>TIFF Directory at offset 0x8
>>
>> Subfile Type: (0 = 0x0)
>>
>> Image Width: 12000 Image Length: 16800
>>
>> Resolution: 400, 400 pixels/inch
>>
>> Bits/Sample: 1
>>
>> Compression Scheme: CCITT Group 4
>>
>> Photometric Interpretation: min-is-white
>>
>> Samples/Pixel: 1
>>
>> Rows/Strip: 16800
>>
>> Planar Configuration: single image plane
>>
>> DocumentName: buzzsaw.com
>>[end output]
>>
>>I need to return the size (width & length) in inches of the Tiff. So I
>>need the information on lines:
>>
>>Image Width: 12000 Image Length: 16800
>>Resolution: 400, 400 pixels/inch
>>
>>Example: 12000 / 400 = 30 & 16800 / 400 = 42, so the Tiff is 30" x 42".
>>
[Kent]
> I would process the data as I find it rather than use all the helper lists. The data format is simple enough that str.split() can be used to extract it. I used float() instead of int() so it will get the correct answer when the dimensions are not whole inches. Something like this:
>
> def getTiffWidthLength():
> for line in os.popen(command).readlines():
> if line.startswith(' Image Width:'):
> data = line.strip().split()
> rawWidth = float(data[2])
> rawLength = float(data[5])
> elif line.startswith(' Resolution:'):
> data = line.strip().split()
> wRes = float(data[1][1:])
> lRes = float(data[2])
>
> width = rawWidth / wRes
> length = rawLength / lRes
> print width, length
>
Kent,
Thank you for the reply! I knew there had to be a different way to do
it, I didn't like the three lists in there either :-)
One thing:
I had to change the line
wRes = float(data[1][1:])
it was returning '00,' (zero, zero, comma) instead of '400', so I
changed it to this:
wRes = float(data[1][:3])
but then I thought, what if I get a tif with a resolution that is <>
than 3 digits?
So I opted for this
wRes = float(data[1].strip(','))
which seems to work fine.
Thanks again!!
Bill
More information about the Tutor
mailing list