#!/usr/bin/env python

from PIL import Image
import numpy as np

width, height = (20, 30)

max_value = np.iinfo(np.uint16).max
raw_data = np.random.randint(0, max_value, (width, height)).astype(np.uint16)

def rgb_from_int16_PIL(arr):
    """
    converts a 2-d array of uint16 grayscale values to a 24 bit RGB array,
    using PIL
    """
    #i = Image.fromstring("F;8", (width, height), arr)
    i = Image.fromstring('RGB', # mode
                         (width, height), # size
                         arr.tostring(), # data
                         "raw", 
                         'F;16', # raw mode
                         # stride
                         # orientation
        )

    return np.fromstring(i.tostring(), dtype=uint8).reshape((width, height, 3))

rgb_data = rgb_from_int16_PIL(raw_data)