
    9i                     ^    S S/r SSKrSSKJr  SSKJrJr  SS jrSS jr	S	 r
SS
 jrSS jrg)imreadimsave    N)Image   )img_as_ubyteimg_as_uintc                     [        U [        5      (       a6  [        U S5       n[        R                  " U5      n[	        XQUS9sSSS5        $ [        R                  " U 5      n[	        XQUS9$ ! , (       d  f       g= f)aW  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/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/io/_plugins/pil_plugin.pyr   r   	   sb    0 %%!AB!"7C  ZZbw?? s    A,,
A:c                     U R                  5       S     / nSnSn	  U R                  U	5        U n
Ub  X):w  a  U R                  5       S     U	S-  n	M7  U R                  S:X  a  U R                  S	:X  a  Uc  S
nU R                  S:X  ak  Uc  [        U 5      nU(       a  U R                  S5      n
OU R                  S:X  a"  SU R                  ;   a  U R                  S5      n
OwU R                  S5      n
OeU R                  S:X  a  U R                  S5      n
OCSU R                  ;   a  U R                  S5      n
O!U R                  S:X  a  U R                  S5      n
U R                  R                  S5      (       a  U R                  nU R                  R                  S5      (       a  SOSnSU R                  ;   a  UR                  SS5      n[         R"                  " U
R%                  5       U5      n
USSS2   U
l        O[         R(                  " XS9n
UR+                  U
5        U	S-  n	Ub  OGM  [-        U S5      (       a+  U R.                  (       a  U R.                  R1                  5         Uc%  [3        U5      S:  a  [         R(                  " U5      $ U(       a  US   $ U(       a  [5        SU 35      eg! [         a2  nSn[        U5      nSU R                   SU SU 3n[	        U5      eSnAff = f! [         a     M  f = f)zaImport 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   ( FI	A
	JJqM 7<MMOAFA<<5 UZZ3%65=E:: 1%8	c*<<5(^uzz-I!MM&1E!MM%0EZZ3MM#&EEJJMM&)EZZ6!MM%(E::  ((JJE"ZZ0055E5Eejj c3/MM%--/59E"+EK HHU0Ee	Qi l ud3v;?xx	ay	27)<== 
S  (]"1vu~~. /,- .,,063 	
 ''(  		s(   J3 K2 3
K/=-K**K/2
L ?L c                 &   U R                   S:w  a  [        S5      e[        R                  " U R	                  5       5      R                  S5      nU R                  5       u  p#XUS-    n[        R                  " [        R                  " U5      S5      $ )zReturn 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      sz     ~~?@@ jj--/088AG&&(KED1H-M ;;rww}-q11    c                    U R                   S:X  a!  [        U 5      n SSS.U R                  S      nOUS;   a  SnSnU R                  R                  S	:X  a  [        U 5      n OfU R                  5       S
:  a7  U R                  5       S:  a#  U R                  [        R                  5      n S=p#O[        U 5      n O[        U 5      n SnSn U R                  5       nU R                   S:X  aA  [        R                  " WU R                   R                  5      n UR#                  USU5        U$ U R                  S   U R                  S   4n [        R"                  " X&U5      nU$ ! [         a    U R                  5       n Nf = f! [         a    UR%                  USU5         U$ f = f! [         a    [        R$                  " X&U5      n U$ f = f)zPExport 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    xx1}3V$SYYq\2	~	%	99>>S c"CWWY_a**RXX&C""D9 c"C 3	&{{} xx1}YYy#%%++.	5LLud3 I yy|SYYq\2	CLAB I  &||~&  	5MM,t4 I	5  	C!!$\BBI	Cs6   	E" F 	F& "E?>E?F#"F#&!G
Gc                 4   [        U [        5      (       d  Uc  Sn[        U [        5      (       a&  U R                  5       R                  S5      (       a  Sn[        R
                  " U5      nUR                  R                  S:X  a  UR                  [        R                  5      nUR                  S;  a  [        SUR                   35      eUR                  S:X  a  UR                  S   S	;  a  [        S
5      e[        XS9nUR                  " U 4SU0UD6  g)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
yy~~jj"
xxv:399+FGG
xx1}99Q<v%IJJ

4CHHU0:00rW   )NN)N)__all__numpyr9   PILr   utilr   r   r   r   r2   rp   r    rW   r   <module>r|      s6   X
   -@BU>p240f61rW   