title: Python: reading 12-bit binary files
body: <p>I am trying to read 12-bit binary files containing images (a video) using Python 3.</p>
  
  <p>To read a similar file but encoded in 16 bits, the following works very well:</p>
  
  <pre><code>import numpy as np
  images = np.memmap(filename_video, dtype=np.uint16, mode='r', shape=(nb_frames, height, width))
  </code></pre>
  
  <p>where filename_video is the file and nb_frames, height, and width characteristics of the video that can be read from another file. By 'working very well' I mean fast: reading a 640x256 video that has 140 frames takes about 1 ms on my computer.</p>
  
  <p>As far as I know I cannot use this when the file is encoded in 12 bits because there is no uint12 type. So what I am trying to do is to read a 12-bit file and store it in a 16-bit uint array. The following, taken from (<a href="https://stackoverflow.com/questions/37797779/python-reading-12-bit-packed-binary-image">Python: reading 12 bit packed binary image</a>), works:</p>
  
  <pre><code>with open(filename_video, 'rb') as f:
      data=f.read()
  images=np.zeros(int(2*len(data)/3),dtype=np.uint16)
  ii=0
  for jj in range(0,int(len(data))-2,3):
      a=bitstring.Bits(bytes=data[jj:jj+3],length=24)
      images[ii],images[ii+1] = a.unpack('uint:12,uint:12')
      ii=ii+2
  images = np.reshape(images,(nb_frames,height,width))
  </code></pre>
  
  <p>However, this is very slow: reading a 640x256 video thas has only 5 frames takes about 11.5 s with my machine. Ideally I would like to be able to read 12-bit files as efficiently as I can read 8 or 16-bit files using memmap. Or at least not 10^5 times slower. How could I speed things up ?</p>
  
  <p>Here is a file example:
  <a href="http://s000.tinyupload.com/index.php?file_id=26973488795334213426" rel="noreferrer">http://s000.tinyupload.com/index.php?file_id=26973488795334213426</a>
  (nb_frames=5, height=256, width=640). </p>