Skip to content

Instantly share code, notes, and snippets.

@benjeffery
Last active August 29, 2015 14:15
Show Gist options
  • Save benjeffery/f292c082d5299e58de0e to your computer and use it in GitHub Desktop.
Save benjeffery/f292c082d5299e58de0e to your computer and use it in GitHub Desktop.
mat2png
#USAGE: python mat2png FILENAME.mat MATRIXNAME
import h5py
import sys
import png
import numpy as np
mat = h5py.File(sys.argv[1], 'r')
print mat['OutputStructure'].keys()
print mat['OutputStructure']['PreProcess'].keys()
matrix = mat['OutputStructure'][sys.argv[2]][::]
matrix = matrix.astype(np.float)
print matrix.shape
m_min, m_max = np.amin(matrix), np.amax(matrix)
matrix = np.clip(matrix, m_min, m_max)
matrix = matrix - m_min
matrix = matrix / ((m_max - m_min)/255)
matrix = matrix.astype(np.uint8)
matrix = matrix.reshape(3456, 4096*4)
f = open(sys.argv[1]+sys.argv[2].split('/')[-1]+'.png', 'wb')
w = png.Writer(4096, 3456, alpha=True, compression=5)
w.write(f, matrix)
f.close()
f = open(sys.argv[1]+sys.argv[2].split('/')[-1]+'.scale','w')
f.write(str(m_min) + ':' + str(((m_max - m_min)/255)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment