๓
;c]c           @` sผ  d  Z  d d l m Z m Z m Z d d l m Z d d l Z d d l Z	 d d l
 m Z m Z d d l
 m Z d	 Z d
 Z e j d  Z i d d 6d d 6Z xV e d g e d   D]; \ Z Z d e e e <e rน d e e e j   d <qน qน Wd d  Z d d  Z d d d  Z d d d  Z d d  Z d d  Z d e d  Z d d  Z  d d  Z! d d  Z" d e d  Z# d d  Z$ e Z% e Z& e Z' e  Z( e" Z) e$ Z* d S(   sN  
These functions represent imageio's main interface for the user. They
provide a common API to read and write image data for a large
variety of formats. All read and write functions accept keyword
arguments, which are passed on to the format that does the actual work.
To see what keyword arguments are supported by a specific format, use
the :func:`.help` function.

Functions for reading:

  * :func:`.imread` - read an image from the specified uri
  * :func:`.mimread` - read a series of images from the specified uri
  * :func:`.volread` - read a volume from the specified uri
  * :func:`.mvolread` - read a series of volumes from the specified uri

Functions for saving:

  * :func:`.imwrite` - write an image to the specified uri
  * :func:`.mimwrite` - write a series of images to the specified uri
  * :func:`.volwrite` - write a volume to the specified uri
  * :func:`.mvolwrite` - write a series of volumes to the specified uri

More control:

For a larger degree of control, imageio provides functions
:func:`.get_reader` and :func:`.get_writer`. They respectively return an
:class:`.Reader` and an :class:`.Writer` object, which can
be used to read/write data and meta data in a more controlled manner.
This also allows specific scientific formats to be exposed in a way
that best suits that file-format.

----

All read-functions return images as numpy arrays, and have a ``meta``
attribute; the meta-data dictionary can be accessed with ``im.meta``.
To make this work, imageio actually makes use of a subclass of
``np.ndarray``. If needed, the image can be converted to a plain numpy
array using ``np.asarray(im)``.

----

Supported resource URI's:

All functions described here accept a URI to describe the resource to
read from or write to. These can be a wide range of things. (Imageio
takes care of handling the URI so that plugins can access the data in
an easy way.)

For reading and writing:

* a normal filename, e.g. ``'c:\foo\bar.png'``
* a file in a zipfile, e.g. ``'c:\foo\bar.zip\eggs.png'``
* a file object with a ``read()`` / ``write()`` method.

For reading:

* an http/ftp address, e.g. ``'http://example.com/foo.png'``
* the raw bytes of an image file
* ``get_reader("<video0>")`` to grab images from a (web) camera.
* ``imread("<screen>")`` to grab a screenshot (on Windows or OS X).
* ``imread("<clipboard>")`` to grab an image from the clipboard (on Windows).

For writing one can also use ``'<bytes>'`` or ``imageio.RETURN_BYTES`` to
make a write function return the bytes instead of writing to a file.

Note that reading from HTTP and zipfiles works for many formats including
png and jpeg, but may not work for all formats (some plugins "seek" the
file object, which HTTP/zip streams do not support). In such a case one
can download/extract the file first. For HTTP one can use something
like ``imageio.imread(imageio.core.urlopen(url).read(), '.gif')``.

i    (   t   absolute_importt   print_functiont   division(   t   NumberNi   (   t   Requestt   RETURN_BYTESi   (   t   formatst   256MBt   1GBs"   ^(\d+\.?\d*)\s*([kKMGTPEZY]?i?)B?$t    t   kMGTPEZYi่  i   t   ic         C` sต   |  s
 d  S|  t k r | St |  t  r- |  St j |   } | d  k r` t d j |     n  | j   \ } } y t	 |  t
 | SWn& t k
 rฐ t d j |    n Xd  S(   NsE   Memory size could not be parsed (is your capitalisation correct?): {}sE   Memory size unit not recognised (is your capitalisation correct?): {}(   t   Nonet   Truet
   isinstanceR   t   mem_ret   matcht
   ValueErrort   formatt   groupst   floatt   sizest   KeyError(   t   argt   defaultR   t   numt   unit(    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt	   to_nbytesd   s$    c         C` s%   |  s t  t  n t  t |   d S(   s;   help(name=None)

    Print the documentation of the format specified by name, or a list
    of supported formats if name is omitted.

    Parameters
    ----------
    name : str
        Can be the name of a format, a filename extension, or a full
        filename. See also the :doc:`formats page <formats>`.
    N(   t   printR   (   t   name(    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   help   s    t   ?c         K` sj   t  |  d | |  } | d k	 r/ t | } n t j |  } | d k r] t d |   n  | j |  S(   sฑ   get_reader(uri, format=None, mode='?', **kwargs)

    Returns a :class:`.Reader` object which can be used to read data
    and meta data from the specified file.

    Parameters
    ----------
    uri : {str, pathlib.Path, bytes, file}
        The resource to load the image from, e.g. a filename, pathlib.Path,
        http address or file object, see the docs for more info.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    mode : {'i', 'I', 'v', 'V', '?'}
        Used to give the reader a hint on what the user expects (default "?"):
        "i" for an image, "I" for multiple images, "v" for a volume,
        "V" for multiple volumes, "?" for don't care.
    kwargs : ...
        Further keyword arguments are passed to the reader. See :func:`.help`
        to see what arguments are available for a particular format.
    t   rs=   Could not find a format to read the specified file in mode %rN(   R   R   R   t   search_read_formatR   t
   get_reader(   t   uriR   t   modet   kwargst   request(    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyR"      s    c         K` s   |  t  k r5 t | t  r5 t  d | j d  }  n  t |  d | |  } | d k	 rd t | } n t j |  } | d k r t d |   n  | j	 |  S(   s   get_writer(uri, format=None, mode='?', **kwargs)

    Returns a :class:`.Writer` object which can be used to write data
    and meta data to the specified file.

    Parameters
    ----------
    uri : {str, pathlib.Path, file}
        The resource to write the image to, e.g. a filename, pathlib.Path
        or file object, see the docs for more info.
    format : str
        The format to use to write the file. By default imageio selects
        the appropriate for you based on the filename.
    mode : {'i', 'I', 'v', 'V', '?'}
        Used to give the writer a hint on what the user expects (default '?'):
        "i" for an image, "I" for multiple images, "v" for a volume,
        "V" for multiple volumes, "?" for don't care.
    kwargs : ...
        Further keyword arguments are passed to the writer. See :func:`.help`
        to see what arguments are available for a particular format.
    t   .s   . t   ws>   Could not find a format to write the specified file in mode %rN(
   R   R   t   strt   stripR   R   R   t   search_write_formatR   t
   get_writer(   R#   R   R$   R%   R&   (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyR,   ฝ   s    c         K` sN   d | k r t  d   n  t |  | d |  } |  | j d  SWd QXd S(   sY   imread(uri, format=None, **kwargs)

    Reads an image from the specified file. Returns a numpy array, which
    comes with a dict of meta data at its 'meta' attribute.

    Note that the image data is returned as-is, and may not always have
    a dtype of uint8 (and thus may differ from what e.g. PIL returns).

    Parameters
    ----------
    uri : {str, pathlib.Path, bytes, file}
        The resource to load the image from, e.g. a filename, pathlib.Path,
        http address or file object, see the docs for more info.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    kwargs : ...
        Further keyword arguments are passed to the reader. See :func:`.help`
        to see what arguments are available for a particular format.
    R$   s<   Invalid keyword argument "mode", perhaps you mean "pilmode"?R   i    N(   t	   TypeErrort   readt   get_data(   R#   R   R%   t   reader(    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   imread์   s    c         K` sอ   t  |  } t j |  } t j | j t j  sN t d j | j    nC | j	 d k r` n1 | j	 d k r | j
 d d	 k r n t d   t |  | d |  } |  | j |  Wd QX| j j   S(
   sฉ   imwrite(uri, im, format=None, **kwargs)

    Write an image to the specified file.

    Parameters
    ----------
    uri : {str, pathlib.Path, file}
        The resource to write the image to, e.g. a filename, pathlib.Path
        or file object, see the docs for more info.
    im : numpy.ndarray
        The image data. Must be NxM, NxMx3 or NxMx4.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    kwargs : ...
        Further keyword arguments are passed to the writer. See :func:`.help`
        to see what arguments are available for a particular format.
    s   Image is not numeric, but {}.i   i   i   i   s+   Image must be 2D (grayscale, RGB, or RGBA).R   N(   i   i   i   (   t   typet   npt
   asanyarrayt
   issubdtypet   dtypet   numberR   R   t   __name__t   ndimt   shapeR,   t   append_dataR&   t
   get_result(   R#   t   imR   R%   t   imtt   writer(    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   imwrite  s    "c   	      K` s   t  |  | d |  } t | t  } g  } d } x_ | D]W } | j |  | | j 7} | r7 | | k r7 g  | (t d j t |     q7 q7 W| S(   s   mimread(uri, format=None, memtest="256MB", **kwargs)

    Reads multiple images from the specified file. Returns a list of
    numpy arrays, each with a dict of meta data at its 'meta' attribute.

    Parameters
    ----------
    uri : {str, pathlib.Path, bytes, file}
        The resource to load the images from, e.g. a filename,pathlib.Path,
        http address or file object, see the docs for more info.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    memtest : {bool, int, float, str}
        If truthy, this function will raise an error if the resulting
        list of images consumes greater than the amount of memory specified.
        This is to protect the system from using so much memory that it needs
        to resort to swapping, and thereby stall the computer. E.g.
        ``mimread('hunger_games.avi')``.

        If the argument is a number, that will be used as the threshold number
        of bytes.

        If the argument is a string, it will be interpreted as a number of bytes with
        SI/IEC prefixed units (e.g. '1kB', '250MiB', '80.3YB').

        - Units are case sensitive
        - k, M etc. represent a 1000-fold change, where Ki, Mi etc. represent 1024-fold
        - The "B" is optional, but if present, must be capitalised

        If the argument is True, the default will be used, for compatibility reasons.

        Default: '256MB'
    kwargs : ...
        Further keyword arguments are passed to the reader. See :func:`.help`
        to see what arguments are available for a particular format.
    t   Ii    s   imageio.mimread() has read over {}B of image data.
Stopped to avoid memory problems. Use imageio.get_reader(), increase threshold, or memtest=False(   R.   R   t   MEMTEST_DEFAULT_MIMt   appendt   nbytest   RuntimeErrorR   t   int(	   R#   R   t   memtestR%   R0   t   nbyte_limitt   imsRD   R=   (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   mimread9  s    (c      	   K` s  t  |  | d |  } d } | พ xถ | D]ฎ } t |  } t j |  } t j | j t j  s} t d j | j	    nC | j
 d k r n1 | j
 d k rด | j d d k rด n t d   | j |  | d 7} q) WWd	 QX| s๖ t d
   n  | j j   S(   sส   mimwrite(uri, ims, format=None, **kwargs)

    Write multiple images to the specified file.

    Parameters
    ----------
    uri : {str, pathlib.Path, file}
        The resource to write the images to, e.g. a filename, pathlib.Path
        or file object, see the docs for more info.
    ims : sequence of numpy arrays
        The image data. Each array must be NxM, NxMx3 or NxMx4.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    kwargs : ...
        Further keyword arguments are passed to the writer. See :func:`.help`
        to see what arguments are available for a particular format.
    RA   i    s   Image is not numeric, but {}.i   i   i   i   s+   Image must be 2D (grayscale, RGB, or RGBA).Ns   Zero images were written.(   i   i   i   (   R,   R2   R3   R4   R5   R6   R7   R   R   R8   R9   R:   R;   RE   R&   R<   (   R#   RI   R   R%   R?   t   writtenR=   R>   (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   mimwritex  s$    "c         K` s3   t  |  | d |  } |  | j d  SWd QXd S(   sห   volread(uri, format=None, **kwargs)

    Reads a volume from the specified file. Returns a numpy array, which
    comes with a dict of meta data at its 'meta' attribute.

    Parameters
    ----------
    uri : {str, pathlib.Path, bytes, file}
        The resource to load the volume from, e.g. a filename, pathlib.Path,
        http address or file object, see the docs for more info.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    kwargs : ...
        Further keyword arguments are passed to the reader. See :func:`.help`
        to see what arguments are available for a particular format.
    t   vi    N(   R.   R/   (   R#   R   R%   R0   (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   volreadฐ  s    c         K` sอ   t  |  } t j |  } t j | j t j  sN t d j | j    nC | j	 d k r` n1 | j	 d k r | j
 d d k  r n t d   t |  | d |  } |  | j |  Wd QX| j j   S(   sฤ   volwrite(uri, vol, format=None, **kwargs)

    Write a volume to the specified file.

    Parameters
    ----------
    uri : {str, pathlib.Path, file}
        The resource to write the image to, e.g. a filename, pathlib.Path
        or file object, see the docs for more info.
    vol : numpy.ndarray
        The image data. Must be NxMxL (or NxMxLxK if each voxel is a tuple).
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    kwargs : ...
        Further keyword arguments are passed to the writer. See :func:`.help`
        to see what arguments are available for a particular format.
    s   Image is not numeric, but {}.i   i   i    s1   Image must be 3D, or 4D if each voxel is a tuple.RM   N(   R2   R3   R4   R5   R6   R7   R   R   R8   R9   R:   R,   R;   R&   R<   (   R#   R=   R   R%   R>   R?   (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   volwriteษ  s    "c   	      K` s   t  |  | d |  } t | t  } g  } d } x_ | D]W } | j |  | | j 7} | r7 | | k r7 g  | (t d j t |     q7 q7 W| S(   s   mvolread(uri, format=None, memtest='1GB', **kwargs)

    Reads multiple volumes from the specified file. Returns a list of
    numpy arrays, each with a dict of meta data at its 'meta' attribute.

    Parameters
    ----------
    uri : {str, pathlib.Path, bytes, file}
        The resource to load the volumes from, e.g. a filename, pathlib.Path,
        http address or file object, see the docs for more info.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    memtest : {bool, int, float, str}
        If truthy, this function will raise an error if the resulting
        list of images consumes greater than the amount of memory specified.
        This is to protect the system from using so much memory that it needs
        to resort to swapping, and thereby stall the computer. E.g.
        ``mimread('hunger_games.avi')``.

        If the argument is a number, that will be used as the threshold number
        of bytes.

        If the argument is a string, it will be interpreted as a number of bytes with
        SI/IEC prefixed units (e.g. '1kB', '250MiB', '80.3YB').

        - Units are case sensitive
        - k, M etc. represent a 1000-fold change, where Ki, Mi etc. represent 1024-fold
        - The "B" is optional, but if present, must be capitalised

        If the argument is True, the default will be used, for compatibility reasons.

        Default: '1GB'
    kwargs : ...
        Further keyword arguments are passed to the reader. See :func:`.help`
        to see what arguments are available for a particular format.
    t   Vi    s   imageio.mvolread() has read over {}B of image data.
Stopped to avoid memory problems. Use imageio.get_reader(), increase threshold, or memtest=False(   R.   R   t   MEMTEST_DEFAULT_MVOLRC   RD   RE   R   RF   (	   R#   R   RG   R%   R0   RH   RI   RD   R=   (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   mvolread๕  s    (c         K` s  t  |  | d |  } d } | พ xถ | D]ฎ } t |  } t j |  } t j | j t j  s} t d j | j	    nC | j
 d k r n1 | j
 d k rด | j d d k  rด n t d   | j |  | d 7} q) WWd	 QX| s๖ t d
   n  | j j   S(   s๎   mvolwrite(uri, vols, format=None, **kwargs)

    Write multiple volumes to the specified file.

    Parameters
    ----------
    uri : {str, pathlib.Path, file}
        The resource to write the volumes to, e.g. a filename, pathlib.Path
        or file object, see the docs for more info.
    ims : sequence of numpy arrays
        The image data. Each array must be NxMxL (or NxMxLxK if each
        voxel is a tuple).
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    kwargs : ...
        Further keyword arguments are passed to the writer. See :func:`.help`
        to see what arguments are available for a particular format.
    RP   i    s   Image is not numeric, but {}.i   i   i    s1   Image must be 3D, or 4D if each voxel is a tuple.i   Ns   Zero volumes were written.(   R,   R2   R3   R4   R5   R6   R7   R   R   R8   R9   R:   R;   RE   R&   R<   (   R#   RI   R   R%   R?   RK   R=   R>   (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt	   mvolwrite3  s$    "(+   t   __doc__t
   __future__R    R   R   t   numbersR   t   ret   numpyR3   R	   R   R   R   RB   RQ   t   compileR   R   R   t	   enumeratet   listR   t   sit   upperR   R   R"   R,   R1   R@   RJ   RL   RN   RO   RR   RS   R.   t   savet   imsavet   mimsavet   volsavet   mvolsave(    (    (    s5   lib/python2.7/site-packages/imageio/core/functions.pyt   <module>K   s@   &(/!,?8,>8