在python中使用字节

时间:2012-05-05 13:13:43

标签: python byte processing

我正在尝试将一个processing.org应用程序移植到python中并遇到一些困难。

我需要在python中写这个:

int[][] elevation; // elevations in meters
float[][] felevation; // scaled to range [0,1] where 1=max
int maxheight;

void setup(){
size(600,600,P2D);

// read srtm binary file
elevation=new int[1201][1201];
felevation=new float[1201][1201];
byte b[] = loadBytes("N30W091.hgt"); // THIS IS A BINARY FILE
int ix=0;
maxheight=0;
for (int row=0;row<1201;row++) {
  for (int col=0;col<1201;col++) {
    // bytes are signed, from -128 to 127, converts to unsigned...
    int hi = b[ix] & 0xff; 
    int lo = b[ix+1] & 0xff; 
    int el=(int)((hi<<8)|lo); // big endian!
    elevation[row][col]=el;
    if (el>maxheight && el<32000) maxheight=el; 
    ix+=2;
   }
}

......等等

到目前为止我所做的是:

elevation = [[],[]]
maxheight=0

b = open("C:\\Users\\CNA\\sketchbook\\_SRTM\\data\\N59E010.hgt","rb")
fin = b.read(1)
print(len(fin))
ix = 0
for row in range(0,1201):
    for col in range(0,1201):
        hi = (fin[ix]   + 0xff)
        lo = (fin[ix+1] + 0xff)

我总是得到

Traceback (most recent call last):

  File "C:\Users\CNA\workspace\Revitter\PatternAsignment.py", line 16, in <module>

TypeError: unsupported operand type(s) for +: 'str' and 'int'

任何想法? ..我是python的新手,我没有使用字节的经验...

3 个答案:

答案 0 :(得分:5)

习惯性翻译将以完全不同的方式发挥作用。

在原始代码中,你做了一堆bit-twiddling将两个字节值转换为单个数值。在Python中,有内置功能:使用struct模块。事实证明,该模块已经构建为一次读取多个值。

此外,使用正斜杠作为文件名 - 它更容易,并保证可以正常工作。使用with-block确保文件自动正确关闭,并使用列表解析来简化循环 - 停止尝试告诉Python如何构建列表,并只询问所需的列表。

这给了我们:

import struct
with open('C:/Users/CNA/sketchbook/_SRTM/data/N59E010.hgt', 'rb') as data:
    elevation = [
        list(struct.unpack('>1201H', data.read(1201 * 2)))
        for row in range(1201)
    ]
maxheight = max(max(cell for cell in row if cell < 32000) for row in elevation)

你已经完成了。欢迎使用Python:)

答案 1 :(得分:2)

在Python中,'hello'[2]之类的值也是字符串(在本例中为== 'l')。您需要使用ord将它们转换为整数,然后使用chr将其转换为字符串。

elevation = [[],[]]
maxheight=0

b = open("C:\\Users\\CNA\\sketchbook\\_SRTM\\data\\N59E010.hgt","rb")
fin = b.read() # you probably need to read more than 1 byte, this will read whole file
print(len(fin))
ix = 0
for row in range(0,1201):
    for col in range(0,1201):
        hi = (ord(fin[ix])   + 0xff) # ord returns unsigned integer, so you probably don't need to convert it
        lo = (ord(fin[ix+1]) + 0xff)
        el = (hi << 8) | lo

请参阅:http://docs.python.org/library/functions.html

答案 2 :(得分:0)

我不确定我的.reshape语法是否正确(将进一步测试) - 但非常类似的东西应该满足您的需求:

import numpy

def readHgtFile(fname, h=1201, w=1201):
    with open(fname, 'rb') as inf:
        return numpy.fromfile(inf, dtype=[('height', '>u2')], count=h*w).reshape((h,w))

def main():
    elevation = readHgtFile('N30W091.hgt')
    maxheight = elevation.max()

if __name__=="__main__":
    main()