Menu

[r6320]: / trunk / py4science / book / snippets / load_binary.ipy  Maximize  Restore  History

Download this file

14 lines (13 with data), 485 Bytes

# open a file as "read binary" and read it into a string
In [1]: s = file('data/images/r1025.ima', 'rb').read()
# the string is length 256*256*2 = 131072
In [2]: len(s)
Out[2]: 131072
# the data are 2 byte / 16 bit integers
# fromstring converts them to array
In [3]: im = nx.fromstring(s, nx.Int16)
# reshape the array to 256x256
In [4]: im.shape = 256,256
# and plot it with matplotlib's imshow function
In [5]: imshow(im)
Out[5]: <matplotlib.image.AxesImage instance at 0xb659230c>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.