title: convert raw binary 8 bit unsigned file to 16 bit unsigned with the python imaging library
body: <p>I have an image file that is a grayscale 8 bit unsigned integer raw binary file and I need to convert it to a 16 bit file and keep it in raw binary.  It is relatively easy to go from 16 to 8 because you are just cutting off information but I am curious how I can go the other way.</p>
  
  <p>To be specific I have an image that is going into a processor written in C++ and the processor only takes 16 bit unsigned integer image files so I need to convert my 8 bit file into a 16 bit one.  I have been doing some processing with the Python Imaging Library but haven't been able to find this specific function.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>I followed cgohlke's advice and have the following code that seems logical but it is not accepting my 'final' variable because of the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Patrick\workspace\colorCorrect\src\editGrayscale.py", line 36, in &lt;module&gt;
      u1 = np.fromfile(final, 'uint8')
  TypeError: file() argument 1 must be encoded string without NULL bytes, not str
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>import Image
  import numpy as np
  
  fileName = raw_input("Enter a file name: ")
  saveFile = raw_input("Enter a new save file name: ")
  
  with open(fileName, 'rb') as f:
      im = Image.fromstring('L', (3032, 2016), f.read()) # also try 'L;16B', 'I;16', and 'I;16B'
      changed = im.point(lambda i: i/.4)    
  
  final = changed.tostring()
  
  np.arange(256).astype('uint8').tofile(final)
  
  u1 = np.fromfile(final, 'uint8')
  u2 = u1.astype('uint16')
  u2 *= 257  # scale to full 16 bit range
  u2.tofile(saveFile)
  </code></pre>