o
    :ήcy                      @   s   d dgZ ddlZddlmZ ddlmZmZ ddl	m
Z
mZ eeedk r4dd	lmZ ed
dd ddd ZdddZdd ZdddZdddZdS )imreadimsave    N)version)Image__version__   )img_as_ubyteimg_as_uintz8.1.2)warnzYour installed pillow version is < 8.1.2. Several security issues (CVE-2021-27921, CVE-2021-25290, CVE-2021-25291, CVE-2021-25293, and more) have been fixed in pillow 8.1.2 or higher. We recommend to upgrade this library.   )
stacklevelc                 K   sh   t | tr(t| d}t|}t|||dW  d   S 1 s!w   Y  dS t| }t|||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   E/tmp/pip-target-vg8gfxp4/lib/python/skimage/io/_plugins/pil_plugin.pyr      s   

$
c              
   C   sL  z|   d  W n ty& } zd}t|}d| j||f }t|d}~ww g }d}d}		 z| |	 W n	 ty>   Y nw | }
|durT||	krT|   d  |	d7 }	q-| jdkrd| jdkrd|du rdd}| jd	kr|du rqt	| }|ry| 
d
}
n6| jdkrd| jv r| 
d}
n&| 
d}
n | jdkr| 
d
}
nd| jv r| 
d}
n
| jdkr| 
d}
| jdr| j}| jdrdnd}d| jv r|dd}t|
 |}
|ddd |
_ntj|
|d}
||
 |	d7 }	|durnq.t| dr| jr| j  |du rt|dkrt|S |r|d S |r$t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-librarieszACould not load "%s" 
Reason: "%s"
Please see documentation at: %sN   PNGIuint16PLtransparencyRGBARGB1ACMYKI;16Bz>u2z<u2Sui)r   fpzCould not find image  #%s)getdataIOErrorr   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sitepillow_error_messageerror_messageframes	grayscaler*   framer?   r   r   r   r   5   s~   







6

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   )	r4   r0   r<   asarray
getpalettereshape
getextremaallclosediff)	pil_imagepalettestartstopvalid_paletter   r   r   r5      s   
r5   c                 C   s\  | j dkrt| } ddd| jd  }n7|dv rCd}d}| jjd	kr't| } n$|  d
k r>|  dkr>| t	j
} d }}nt| } nt| } d}d}z|  }W n ty^   |  }Y nw | j dkrt|| jj}z
||d| W |S  ty   ||d| Y |S w | jd | jd f}z
t|||}W |S  ty   t|||}Y |S w )z`Export an ndarray to a PIL object.

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

    r   r"   r!   r      r   )pngr   r&   r   r      r   r   rawr   )ndimr   r?   r   kindr	   maxminastyper<   uint8r>   AttributeErrortostringr   newT	frombytesr=   )arr
format_strr4   	mode_basearray_bufferr   image_shaper   r   r   ndarray_to_pil   sJ   




	rn   c                 K   s   t | ts|du rd}t | tr|  drd}t|}|jjdkr*|tj	}|j
dvr7td|jf |j
dkrG|jd d	vrGt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)r   r   z!Invalid shape for image array: %sr   r   rY   z*Invalid number of channels in image array.)rj   r3   )r   r   lowerr:   r<   
asanyarrayr   r_   rb   rc   r^   r0   r?   rn   save)r   ri   rj   r   imgr   r   r   r      s    #



)NN)N)__all__numpyr<   	packagingr   PILr   r   pil_versionutilr   r	   parsewarningsr
   r   r   r5   rn   r   r   r   r   r   <module>   s    

!W
3