help > .buchar images
Showing 1-2 of 2 posts
Display:
Results per page:
Aug 13, 2018  08:08 PM | Emilly Pereira Emilly
.buchar images
Hi,

How can I open .buchar images in python?
Aug 14, 2018  04:08 PM | Andrew Worth
RE: .buchar images

Originally posted by Emilly Pereira Emilly:
Hi,

How can I open .buchar images in python?


Try the code below (note the name of the file, 'prob00' is hard coded).

Andy.


#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

fName = 'prob00'

# Read image header file which gives: rows, columns, slices, and endian
# (0 is big endian, 1 is little endian)
f = open(fName+'.hdr','rb')
line = f.readline().split()
f.close()
print(line)
rows = int(line[0])
cols = int(line[1])
slis = int(line[2])
endi = int(line[3]) # ignored because a char doesn't change with endian
print('rows = '+str(rows))
print('cols = '+str(cols))
print('slis = '+str(slis))
print('endi = '+str(endi))

f = open(fName+'.buchar','rb')
img_arr=np.fromfile(f,dtype=np.uint8) # uint8, unsigned char = buchar
f.close()
img_arr=img_arr.reshape(slis,rows,cols)
plt.imshow(img_arr[200], cmap=plt.cm.gray)
plt.show()