U
    ?h                     @   s`   d dgZ ddlZddlmZ ddlmZmZ ddd Zddd	Z	d
d Z
dddZdddZdS )imreadimsave    N)Image   )img_as_ubyteimg_as_uintc              
   K   s^   t | trBt| d&}t|}t|||dW  5 Q R  S Q R X nt| }t|||dS dS )a  Load an image from file.

    Parameters
    ----------
    fname : str or file
        File name or file-like-object.
    dtype : numpy dtype object or string specifier
        Specifies data type of array elements.
    img_num : int, optional
        Specifies which image to read in a file with multiple images
        (zero-indexed).
    kwargs : keyword pairs, optional
        Addition keyword arguments to pass through.

    Notes
    -----
    Files are read using the Python Imaging Library.
    See PIL docs [1]_ for a list of supported formats.

    References
    ----------
    .. [1] http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html
    rb)dtypeimg_numN)
isinstancestropenr   pil_to_ndarray)fnamer	   r
   kwargsfim r   P/var/www/html/venv/lib/python3.8/site-packages/skimage/io/_plugins/pil_plugin.pyr   	   s    

"
c              
   C   s|  z|   d  W nN tk
r^ } z0d}t|}d| j d| d| }t|W 5 d}~X Y nX g }d}d}	z| |	 W n tk
r   Y qY nX | }
|dk	r||	kr|   d  |	d7 }	ql| jdkr| jd	kr|dkrd
}| jdkr>|dkrt	| }|r| 
d}
n.| jdkr2d| jkr2| 
d}
n
| 
d}
nF| jdkrV| 
d}
n.d| jkrn| 
d}
n| jdkr| 
d}
| jdr| j}| jdrdnd}d| jkr|dd}t|
 |}
|ddd |
_ntj|
|d}
||
 |	d7 }	|dk	rlqqlt| dr4| jr4| j  |dkrVt|dkrVt|S |rd|d S |rxtd| dS )zqImport a PIL Image object to an ndarray, in memory.

    Parameters
    ----------
    Refer to ``imread``.

    r   zLhttp://pillow.readthedocs.org/en/latest/installation.html#external-librarieszCould not load 'z' 
Reason: 'z'
Please see documentation at: N   PNGIZuint16PLZtransparencyRGBARGB1AZCMYKI;16Bz>u2z<u2Sui)r	   fpzCould not find image  #)ZgetdataOSErrorr   filename
ValueErrorseekEOFErrorformatmode_palette_is_grayscaleconvertinfo
startswithsizeendswithreplacenp
fromstringtobytesshapearrayappendhasattrr$   closelen
IndexError)imager	   r
   esiteZpillow_error_messageerror_messageframesZ	grayscaler"   framer6   r   r   r   r   *   sn    



r   c                 C   sT   | j dkrtdt|  d}|  \}}|||d  }tt|dS )a  Return True if PIL image in palette mode is grayscale.

    Parameters
    ----------
    pil_image : PIL image
        PIL Image that is in Palette mode.

    Returns
    -------
    is_grayscale : bool
        True if all colors in image palette are gray.
    r   z$pil_image.mode must be equal to "P".)r#   r   r   r   )	r+   r'   r3   ZasarrayZ
getpaletteZreshapeZ
getextremaZallclosediff)Z	pil_imagepalettestartstopZvalid_paletter   r   r   r,      s    
r,   c                 C   sf  | j dkr(t| } ddd| jd  }nn|dkrd}d}| jjd	krNt| } q|  d
k r||  dkr|| t	j
} d }}qt| } nt| } d}d}z|  }W n tk
r   |  }Y nX | j dkrt|| jj}z||d| W n$ tk
r   ||d| Y nX nL| jd | jd f}zt|||}W n$ tk
r`   t|||}Y nX |S )z`Export an ndarray to a PIL object.

    Parameters
    ----------
    Refer to ``imsave``.

    r   r   r   r         )Zpngr   r   r   r      r   r   rawr   )ndimr   r6   r	   kindr   maxminastyper3   uint8r5   AttributeErrortostringr   newT	frombytesr4   )arr
format_strr+   Z	mode_baseZarray_bufferr   Zimage_shaper   r   r   ndarray_to_pil   s>    



rY   c                 K   s   t | ts|dkrd}t | tr2|  dr2d}t|}|jjdkrT|tj	}|j
dkrntd|j |j
dkr|jd d	krtd
t||d}|j| fd|i| dS )a;  Save an image to disk.

    Parameters
    ----------
    fname : str or file-like object
        Name of destination file.
    arr : ndarray of uint8 or float
        Array (image) to save.  Arrays of data-type uint8 should have
        values in [0, 255], whereas floating-point arrays must be
        in [0, 1].
    format_str: str
        Format to save as, this is defaulted to PNG if using a file-like
        object; this will be derived from the extension if fname is a string
    kwargs: dict
        Keyword arguments to the Pillow save function (or tifffile save
        function, for Tiff files). These are format dependent. For example,
        Pillow's JPEG save function supports an integer ``quality`` argument
        with values in [1, 95], while TIFFFile supports a ``compress``
        integer argument with values in [0, 9].

    Notes
    -----
    Use the Python Imaging Library.
    See PIL docs [1]_ for a list of other supported formats.
    All images besides single channel PNGs are converted using `img_as_uint8`.
    Single Channel PNGs have the following behavior:
    - Integer values in [0, 255] and Boolean types -> img_as_uint8
    - Floating point and other integers -> img_as_uint16

    References
    ----------
    .. [1] http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html
    Nr   z.pngb)rI   r   zInvalid shape for image array: r   rI   rG   z*Invalid number of channels in image array.)rX   r*   )r   r   lowerr1   r3   Z
asanyarrayr	   rM   rP   rQ   rL   r'   r6   rY   save)r   rW   rX   r   Zimgr   r   r   r      s     #



)NN)NN)N)N)__all__numpyr3   ZPILr   utilr   r   r   r   r,   rY   r   r   r   r   r   <module>   s   
!
V
3