Home > Software design >  How do I create image from binary data BSQ?
How do I create image from binary data BSQ?

Time:12-02

I've got a problem. I'm trying create image from binary data which I got from hyperspectral camera. The file which I have is in BSQ uint16 format. From the documentation I found out that images contained in the file (.dat) have a resolution of 1024x1024 and there are 24 images in total. The whole thing is to form a kind of "cube" which I want use in the future to creat multi-layered orthomosaic.

I would also like to add that I am completely new in python but I try to be up to date with everything I need. I hope that everything what I have written is clear and uderstandable.

At first I tried to use Numpy liblary to creating 3D array but ended up with an arrangement of random pixels.

from PIL import Image
import numpy as np

file=open('Sequence 1_000021.dat','rb')

myarray=np.fromfile(file,dtype=np.uint16)

print('Size of new array',":", len(myarray))

con_array=np.reshape(myarray,(24,1024,1024),'C')

naPIL=Image.fromarray(con_array[1,:,:])
naPIL.save('naPIL.tiff')

The result: enter image description here

Example of image which I want to achieve (thumbnail): enter image description here

CodePudding user response:

As suspected it's just byte order, I get a sensible looking image when running the following code in a Jupyter notebook:

import numpy as np
from PIL import Image

# open as big-endian, convert to native order, then reshape as appropriate
raw = np.fromfile(
  './Sequence 1_000021.dat', dtype='>u2'
).astype('uint16').reshape((24, 1024, 1024))

# display inline
Image.fromarray(raw[1,:,:])
  • Related