o
    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   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   Y/var/www/html/scripts/venv/lib/python3.10/site-packages/skimage/io/_plugins/pil_plugin.pyr   	   s   

$
c              
   C   sV  z|   d  W n" ty* } zd}t|}d| j d| d| }t|d}~ww g }d}d}		 z| |	 W n	 tyB   Y nw | }
|durX||	krX|   d  |	d7 }	q1| jdkrh| jd	krh|du rhd
}| jdkr|du rut	| }|r}| 
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q2t| 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-librarieszCould not load 'z' 
Reason: 'z'
Please see documentation at: N   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  #)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sitepillow_error_messageerror_messageframes	grayscaler%   framer:   r   r   r   r   *   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   )	r/   r+   r7   asarray
getpalettereshape
getextremaallclosediff)	pil_imagepalettestartstopvalid_paletter   r   r   r0      s   
r0   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         )pngr   r!   r   r      r   r   rawr   )ndimr   r:   r	   kindr   maxminastyper7   uint8r9   AttributeErrortostringr   newT	frombytesr8   )arr
format_strr/   	mode_basearray_bufferr   image_shaper   r   r   ndarray_to_pil   sJ   




	rj   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 |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)rV   r   zInvalid shape for image array: r   rV   rT   z*Invalid number of channels in image array.)rf   r.   )r   r   lowerr5   r7   
asanyarrayr	   r[   r^   r_   rZ   r+   r:   rj   save)r   re   rf   r   imgr   r   r   r      s    #



)NN)N)__all__numpyr7   PILr   utilr   r   r   r   r0   rj   r   r   r   r   r   <module>   s    

!V
3