import PIL.Image # http://www.pythonware.com/products/pil/
import numpy     # http://numpy.scipy.org/
import sys

print 'Python version:',sys.version
print 'numpy version: ',numpy.__version__
print 'PIL version:   ',PIL.Image.VERSION

# source  = numpy.arange(0,12,dtype=float)
source  = numpy.arange(0,12,dtype=int)
source  = source.reshape((3,4))
print 'numpy source array:\n',source
print 'numpy source array shape:', source.shape
extrema = source.min(), source.max()

image   = PIL.Image.fromarray(source)
# image   = PIL.Image.fromarray(source.transpose()) # ~~~ TBD: why is transpose necessary ?
print 'PIL image size:',image.size
# size    = list(source.shape)
# size.reverse() # (width,height)
# assert image.size == size,\
#    '''
#    numpy image shape (height,width): %s
#    PIL   image size  (width,height): %s
#    ''' % (source.shape, image.size)
assert image.getextrema() == extrema,\
   '''
   numpy image extrema (minimum,maximum): %s
   PIL   image extrema (minimum,maximum): %s
   ''' % (extrema, image.getextrema())

target = numpy.asarray(image)
print 'final numpy array created from PIL Image:\n',target
assert (target == source).all()
