Created
October 18, 2016 07:07
-
-
Save laanlabs/a1f600ddfa336d53b1558c68ff132201 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# In[1]: | |
# In[7]: | |
import struct | |
import numpy as np | |
from scipy.misc.pilutil import toimage | |
from PIL import Image | |
filename = 'mesh.msh' | |
f = open(filename, 'rb') | |
hdr = struct.unpack('BBBB', f.read(4)) | |
assert hdr == (0,0,0,2), "no header found" | |
header2 = struct.unpack('cccccccc', f.read(8)) | |
print "".join(reversed(header2)) | |
header2000 = struct.unpack('BBBB', f.read(4)) | |
print header2000 | |
w = struct.unpack('i', f.read(4))[0] | |
h = struct.unpack('i', f.read(4))[0] | |
print '' | |
print "Width: {} Height: {}".format(w,h) | |
hzero = struct.unpack('d', f.read(8)) | |
## read image data | |
length = w*h | |
dx_arr = np.zeros(length, np.float32) | |
dy_arr = np.zeros(length, np.float32) | |
for idx in range(0, length): | |
dx_arr[idx] = struct.unpack('f', f.read(4))[0] | |
dy_arr[idx] = struct.unpack('f', f.read(4))[0] | |
dx_arr = dx_arr.reshape(h,w) | |
dy_arr = dy_arr.reshape(h,w) | |
# In[6]: | |
### Visualize as RGB image / save to jpg ##### | |
arr_rgb = np.zeros((h,w,3), np.uint8) | |
scale = 10 | |
arr_rgb[:,:,0] = np.round(np.clip(128.0 + scale * dx_arr, 0, 255)) | |
arr_rgb[:,:,1] = np.round(np.clip(128.0 + scale * dy_arr, 0, 255)) | |
print arr_rgb.max(), arr_rgb.min() | |
imshow(arr_rgb) | |
# In[4]: | |
image = toimage(arr_rgb) | |
filename = 'out.jpg' | |
image = image.resize((128, 128), Image.BICUBIC) | |
image.save(filename, 'JPEG', quality=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment