
    k h                     V    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y)imreadimsave    N)Image   )img_as_ubyteimg_as_uintc                     t        | t              r9t        | d      5 }t        j                  |      }t	        |||      cddd       S t        j                  |       }t	        |||      S # 1 sw Y   yxY w)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         z/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/skimage/io/_plugins/pil_plugin.pyr   r   	   sq    0 %% 	D!AB!"E7C	D 	D ZZbw??	D 	Ds   #A--A6c                    	 | j                         d    g }d}d}		 	 | j                  |	       | }
|||	k7  r| j                         d    |	dz  }	5| j                  dk(  r| j                  d	k(  r|d
}| j                  dk(  rb|t        |       }|r| j                  d      }
n| j                  dk(  r d| j                  v r| j                  d      }
ns| j                  d      }
na| j                  dk(  r| j                  d      }
n@d| j                  v r| j                  d      }
n | j                  dk(  r| j                  d      }
| j                  j                  d      r}| j                  }| j                  j                  d      rdnd}d| j                  v r|j                  dd      }t!        j"                  |
j%                         |      }
|ddd   |
_        nt!        j(                  |
|      }
|j+                  |
       |	dz  }	|nt-        | d      r&| j.                  r| j.                  j1                          |#t3        |      dkD  rt!        j(                  |      S |r|d   S |rt5        d|       y# t        $ r2}d}t        |      }d| j                   d| d| }t	        |      d}~ww xY w# t        $ r Y w xY w)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   ( FI	A
	JJqM 7a<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5E5Eejj c3/MM%--/59E"+EK HHU%0Ee	Qi l ud3v;?xx	ay	27)<== 
S  (]"1vu~~. /,- .,,063 	
 ''(  		s(   I= J; =	J8-J33J8;	KKc                 "   | j                   dk7  rt        d      t        j                  | j	                               j                  d      }| j                         \  }}|||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~     ~~?@@ jj--/088AG&&(KE4ED1H-M ;;rww}-q11    c                    | j                   dk(  r!t        |       } ddd| j                  d      }n|dv rd}d}| j                  j                  d	k(  rt        |       } ne| j                         d
k  r7| j                         dk\  r$| j                  t        j                        } dx}}nt        |       } nt        |       } d}d}	 | j                         }| j                   dk(  r@t        j                  | j                   j                        }	 |j#                  |d|       |S | j                  d   | j                  d   f}	 t        j"                  |||      }|S # t        $ r | j                         }Y w xY w# t        $ r |j%                  |d|       Y |S w xY w# t        $ r t        j$                  |||      }Y |S w xY 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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 E: F E76E7:FF G Gc                    t        | t              s|d}t        | t              r!| j                         j                  d      rd}t	        j
                  |      }|j                  j                  dk(  r|j                  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| y)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CHHU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   