def to_image(img, image_type=None, meta_data=None, **kwargs):
<EOL>meta_data = pimms.merge({} if meta_data is None else meta_data, kwargs)<EOL>if image_type is None: image_type = '<STR_LIT>'<EOL>image_type = to_image_type(image_type)<EOL>if is_tuple(img):<EOL><INDENT>if   len(img) == <NUM_LIT:1>: (img,aff,mdat) = (img[<NUM_LIT:0>], None, None)<EOL>elif len(img) == <NUM_LIT:2>: (img,aff,mdat) = (img[<NUM_LIT:0>], None, img[<NUM_LIT:1>])<EOL>elif len(img) == <NUM_LIT:3>: (img,aff,mdat) = img<EOL>else: raise ValueError('<STR_LIT>')<EOL><DEDENT>else: (aff,mdat) = (None,None)<EOL>try: (img,aff0,mdat0) = (img.dataobj, img.affine, to_image_meta_data(img))<EOL>except Exception: (aff0,mdat0) = (None, {})<EOL>if aff is None and mdat is not None:<EOL><INDENT>try:    (aff, mdat) = (to_affine(mdat, <NUM_LIT:3>), {})<EOL>except Exception: pass<EOL><DEDENT>mdat = dict(pimms.merge(mdat0, {} if mdat is None else mdat, meta_data))<EOL>if aff is not None: mdat['<STR_LIT>'] = to_affine(aff, <NUM_LIT:3>)<EOL>if aff0 is not None and '<STR_LIT>' not in mdat: mdat['<STR_LIT>'] = to_affine(aff0, <NUM_LIT:3>)<EOL>return image_type.create(img, meta_data=mdat)<EOL>
to_image(array) yields a Nifti1Image of the given array with default meta-data.
to_image(array, image_type) yields an image object of the given type; image_type may either be
  an image class or a class name (see supported types below).
to_image((array, meta_data)) uses the given mapping of meta-data to fill in the image's
  meta-data; note that meta_data may simply be an affine transformation matrix.
to_image((array, affine, meta_data)) uses the given affine specifically (the given affine
  overrides any affine included in the meta_data).

Note that the array may optionally be an image itself, in which case its meta-data is used as a
starting point for the new meta-data. Any meta-data passed as a tuple overwrites this meta-data,
and any meta-data passed as an optional argument overwrites this meta-data in turn.

The first optional argument, specifying image_type is as an image type if possible, but if a
meta-data mapping is passed as the first argument it is used as such; otherwise, the optional
third argument is named meta_data, and any additional keyword arguments passed to to_image are
merged into this meta_data object left-to-right (i.e., keyword arguments overwrite the meta_data
keys).