
    1-Ph                     X    d dgZ ddlZddlmZ ddlmZmZ ddZddZ	d	 Z
dd
ZddZdS )imreadimsave    N)Image   )img_as_ubyteimg_as_uintc                    t          | t                    rPt          | d          5 }t          j        |          }t	          |||          cddd           S # 1 swxY w Y   dS t          j        |           }t	          |||          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ims         ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/io/_plugins/pil_plugin.pyr   r   	   s    0 % @% 	D!AB!"E7CCC	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D Zbw????s   &AA Ac                    	 |                                  d          nB# t          $ r5}d}t          |          }d| j         d| d| }t	          |          d}~ww xY wg }d}d}		 	 |                     |	           n# t          $ r Y nw xY w| }
|&||	k    r |                                  d          |	dz  }	S| j        dk    r| j        d	k    r|d
}| j        dk    ri|t          |           }|r| 
                    d          }
n| j        dk    rd| j        v r| 
                    d          }
nv| 
                    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          rdnd}d| j        v r|                    dd          }t!          j        |
                                |          }
|ddd         |
_        nt!          j        |
|          }
|                    |
           |	dz  }	|nt-          | d          r | j        r| j                                         |'t3          |          dk    rt!          j        |          S |r|d         S |rt5          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
frombuffertobytesshapearrayappendhasattrr)   closelen
IndexError)imager   r   esitepillow_error_messageerror_messageframes	grayscaler'   framer<   s               r   r   r   *   s`   ( ( ( (]"1vv3u~ 3 3,3 3,03 3 	
 '''( FI	A4	JJqMMMM 	 	 	E	 7a<<MMOOAFA<5  UZ3%6%65=E: 1%88	 1c**<5((^uz-I-I!MM&11EE!MM%00EEZ3MM#&&EEEJMM&))EEZ6!!MM%((E:  (( 		1JE"Z0055@EE5Eej  c3//M%--//599E"+EKK HU%000Ee	Qi4l ud  3v;;??x	 >ay	 ><7<<===> >s&    
A0AA'A= =
B
Bc                 >   | j         dk    rt          d          t          j        |                                                               d          }|                                 \  }}|||dz            }t          j        t          j        |          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   )	r1   r-   r9   asarray
getpalettereshape
getextremaallclosediff)	pil_imagepalettestartstopvalid_palettes        r   r2   r2      s     ~?@@@ j--//0088AAG&&((KE4ED1H,-M ;rw}--q111    c                    | j         dk    r&t          |           } ddd| j        d                  }n|dv rd}d}| j        j        d	k    rt          |           } nw|                                 d
k     r<|                                 dk    r$|                     t          j
                  } dx}}n#t          |           } nt          |           } d}d}	 |                                 }n$# t          $ r |                                 }Y nw xY w| j         dk    r_t          j        || j        j                  }	 |                    |d|           n# t          $ r |                    |d|           Y n\w xY w| j        d         | j        d         f}	 t          j        |||          }n&# t          $ r t          j        |||          }Y nw xY w|S )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astyper9   uint8r;   AttributeErrortostringr   newT	frombytes
fromstring)arr
format_strr1   	mode_basearray_bufferr   image_shapes          r   ndarray_to_pilrp      s    x1}}3V$$SYq\2	~	%	%	9>S  c""CCWWYY__a**RX&&C""D99 c""CC 3	&{{}} & & &||~~& x1}}Yy#%+..	5LLud3333 	5 	5 	5MM,t44444	5 y|SYq\2	C{LAABB 	C 	C 	C!$\BBBBB	CIs6   C' 'DD6E !E21E2F' ' G
	G
c                    t          | t                    s|d}t          | t                    r)|                                                     d          rd}t	          j        |          }|j        j        dk    r|                    t          j	                  }|j
        dvrt          d|j                   |j
        dk    r|j        d         d	vrt          d
          t          ||          } |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: r   r[   rY   z*Invalid number of channels in image array.)rl   r0   )r   r   lowerr7   r9   
asanyarrayr   r`   rc   rd   r_   r-   r<   rp   save)r   rk   rl   r   imgs        r   r   r      s   F eS!! j&8
% %++--"8"8"@"@ 

-

C
y~jj""
xvF39FFGGG
x1}}9Q<v%%IJJJ

4
4
4CCHU00:000000rW   )NN)N)__all__numpyr9   PILr   utilr   r   r   r   r2   rp   r    rW   r   <module>r|      s   X
           - - - - - - - -@ @ @ @BU> U> U> U>p2 2 240 0 0 0f61 61 61 61 61 61rW   