
    0-Ph7                         d Z ddlmZ ddlmZmZmZmZ ddlZddl	Z
ddlZddlmZmZmZ ddlmZmZ ddlmZ d	ej        d
eeef         fdZ G d de          ZdS )a  Read/Write TIFF files using tifffile.

.. note::
    To use this plugin you need to have `tifffile
    <https://github.com/cgohlke/tifffile>`_ installed::

        pip install tifffile

This plugin wraps tifffile, a powerful library to manipulate TIFF files. It
superseeds our previous tifffile plugin and aims to expose all the features of
tifffile.

The plugin treats individual TIFF series as ndimages. A series is a sequence of
TIFF pages that, when combined describe a meaningful unit, e.g., a volumetric
image (where each slice is stored on an individual page) or a multi-color
staining picture (where each stain is stored on an individual page). Different
TIFF flavors/variants use series in different ways and, as such, the resulting
reading behavior may vary depending on the program used while creating a
particular TIFF file.

Methods
-------
.. note::
    Check the respective function for a list of supported kwargs and detailed
    documentation.

.. autosummary::
    :toctree:

    TifffilePlugin.read
    TifffilePlugin.iter
    TifffilePlugin.write
    TifffilePlugin.properties
    TifffilePlugin.metadata

Additional methods available inside the :func:`imopen <imageio.v3.imopen>`
context:

.. autosummary::
    :toctree:

    TifffilePlugin.iter_pages

    )BytesIO)AnyDictOptionalcastN   )	URI_BYTESInitializationErrorRequest)ImagePropertiesPluginV3)	ArrayLikepagereturnc                 f   i }	 | j         d         j        j        |d<   n# t          $ r |cY S w xY w	 | j         d         j        }| j         d         j        }|d         |d         z  |d         |d         z  f|d<   n5# t          $ r Y n)t          $ r t	          j        dt                     Y nw xY w|S )	Ni(  resolution_uniti  i  r      
resolutionzPIgnoring resolution metadata because at least one direction has a 0 denominator.)tagsvalueKeyErrorZeroDivisionErrorwarningswarnRuntimeWarning)r   metadataresolution_xresolution_ys       [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/tifffile_v3.py_get_resolutionr    :   s    H&*in&:&@"##   
y~+y~+ Ol1o-Ol1o-"
     
 
 
	
 	
 	
 	
 	

 Os$    ..A	A< <
B.#B.-B.c                        e Zd ZdZdeddf fdZddddededej        fd	Z	dej        fd
Z
dddededee         fdZeddddedededeeef         fdZddddededefdZd fdZddZ xZS )TifffilePlugina  Support for tifffile as backend.

    Parameters
    ----------
    request : iio.Request
        A request object that represents the users intent. It provides a
        standard interface for a plugin to access the various ImageResources.
        Check the docs for details.
    kwargs : Any
        Additional kwargs are forwarded to tifffile's constructor, i.e.
        to ``TiffFile`` for reading or ``TiffWriter`` for writing.

    requestr   Nc                 p   t                                          |           d | _        |j        j        dk    rS	 t          j        |                                fi || _        d S # t
          j        j        $ r t          d          w xY wt          j
        |                                fi || _        d S )Nrz Tifffile can not read this file.)super__init___fhmodeio_modetifffileTiffFileget_fileTiffFileErrorr
   
TiffWriter)selfr#   kwargs	__class__s      r   r'   zTifffilePlugin.__init__g   s    !!!<3&&N#,W-=-=-?-?JJ6JJ$2 N N N)*LMMMN  *7+;+;+=+=HHHHDHHHs   )A& &$B
)indexr   r3   r   c                P   d|vr||d<   n|t          d          |d|v rt          d          d|v r|                    d          }n|nd}|t          u r,|*t          j        d  | j        di |D                       }n!|t          u rdn|} | j        j        dd|i|}|S )	a  Read a ndimage or page.

        The ndimage returned depends on the value of both ``index`` and
        ``page``. ``index`` selects the series to read and ``page`` allows
        selecting a single page from the selected series. If ``index=None``,
        ``page`` is understood as a flat index, i.e., the selection ignores
        individual series inside the file. If both ``index`` and ``page`` are
        ``None``, then all the series are read and returned as a batch.

        Parameters
        ----------
        index : int
            If ``int``, select the ndimage (series) located at that index inside
            the file and return ``page`` from it. If ``None`` and ``page`` is
            ``int`` read the page located at that (flat) index inside the file.
            If ``None`` and ``page=None``, read all ndimages from the file and
            return them as a batch.
        page : int
            If ``None`` return the full selected ndimage. If ``int``, read the
            page at the selected index and return it.
        kwargs : Any
            Additional kwargs are forwarded to TiffFile's ``as_array`` method.

        Returns
        -------
        ndarray : np.ndarray
            The decoded ndimage or page.
        keyNz,Can't use `page` and `key` at the same time.seriesz0Can't use `series` and `index` at the same time.r   c                     g | ]}|S  r8   ).0xs     r   
<listcomp>z'TifffilePlugin.read.<locals>.<listcomp>   s    ???a???    r8   )
ValueErrorpopEllipsisnpstackiterr(   asarray)r0   r3   r   r1   ndimages        r   readzTifffilePlugin.readw   s    <  F5MMKLLL V!3!3OPPPJJx((EEEHh??949+>+>v+>+>???@@GG!X--DD5E&dh&>>e>v>>Gr<   c              +   F   K   | j         j        D ]} |j        di |V  dS )a!  Yield ndimages from the TIFF.

        Parameters
        ----------
        kwargs : Any
            Additional kwargs are forwarded to the TiffPageSeries' ``as_array``
            method.

        Yields
        ------
        ndimage : np.ndarray
            A decoded ndimage.
        Nr8   )r(   r6   rC   )r0   r1   sequences      r   rB   zTifffilePlugin.iter   sG        	- 	-H"(",,V,,,,,,	- 	-r<   F)is_batchrD   rH   c                R   |st          j        |          dddf         }|D ]} | j        j        |fi | | j        j        t          k    rY| j                                         t          t          | j        
                                          }|                                S dS )a_  Save a ndimage as TIFF.

        Parameters
        ----------
        ndimage : ArrayLike
            The ndimage to encode and write to the ImageResource.
        is_batch : bool
            If True, the first dimension of the given ndimage is treated as a
            batch dimension and each element will create a new series.
        kwargs : Any
            Additional kwargs are forwarded to TiffWriter's ``write`` method.

        Returns
        -------
        encoded_image : bytes
            If the ImageResource is ``"<bytes>"``, return the encoded bytes.
            Otherwise write returns None.

        Notes
        -----
        Incremental writing is supported. Subsequent calls to ``write`` will
        create new series unless ``contiguous=True`` is used, in which case the
        call to write will append to the current series.

        N)r@   rC   r(   write_request	_uri_typer	   closer   r   r-   getvalue)r0   rD   rH   r1   imagefiles         r   rJ   zTifffilePlugin.write   s    :  	3j))$'2G 	, 	,EDHN5++F++++="i//HNN!7!7!9!9::D==??" 0/r<   T)r3   r   exclude_appliedrQ   c          
      f   |t           ur || j        j        |         j        |         }nI|t           ur || j        j        |         j        d         }n |t           u r|| j        j        |         }nd}i }|| j        j        |d<   t
          j        j        D ]}t          | j        d|z             }||d|z   <   |rvt          | j        |dz             r^t          | j        |dz             }t          |t                    r|                    |d                    |                    |           n~|j        }|                    d |j        D                        |                    |j        |j        |j        d|j        |j        |j        dt+          |          d|j        i           |S )	as  Format-Specific TIFF metadata.

        The metadata returned depends on the value of both ``index`` and
        ``page``. ``index`` selects a series and ``page`` allows selecting a
        single page from the selected series. If ``index=Ellipsis``, ``page`` is
        understood as a flat index, i.e., the selection ignores individual
        series inside the file. If ``index=Ellipsis`` and ``page=None`` then
        global (file-level) metadata is returned.

        Parameters
        ----------
        index : int
            Select the series of which to extract metadata from. If Ellipsis, treat
            page as a flat index into the file's pages.
        page : int
            If not None, select the page of which to extract metadata from. If
            None, read series-level metadata or, if ``index=...`` global,
            file-level metadata.
        exclude_applied : bool
            For API compatibility. Currently ignored.

        Returns
        -------
        metadata : dict
            A dictionary with information regarding the tiff flavor (file-level)
            or tiff tags (page-level).
        Nr   	byteorderis_	_metadatac                 (    i | ]}|j         |j        S r8   )namer   )r9   tags     r   
<dictcomp>z+TifffilePlugin.metadata.<locals>.<dictcomp>$  s    HHHSSXsyHHHr<   )planar_configurationcompression	predictororientationdescription1descriptionsoftwaredatetime)r?   r(   r6   pagesrS   r+   TIFF
FILE_FLAGSgetattrhasattr
isinstancetupleupdatekeyframer   planarconfigr[   r\   r^   r_   r`   r    ra   )	r0   r3   r   rQ   targetr   flag
flag_valueflavor_metadatas	            r   r   zTifffilePlugin.metadata   s   >   T%5X_U+1$7FF(""t| X_U+1!4FFh4#3X^D)FFF>$(H$6H[! 0 	9 	9$TXut|<<
)3& 9'$(D;4F"G"G 9&-dh{8J&K&KO!/599 9 (:;;;; 888	9 _FOOHHFKHHHIIIOO,2,?#)#5!'!1#'$*$7#)#5 &
 
 &f--
 
 
   r<   c          	         |pd}|dt           fv rdn|}|t           u r| j        j        |         }n| j        j        |         j        |         }|t           u r^|\t	          | j        j                  }t          |g|j        R |j        |dt          |          	                    d                    }n|t           u re|t           u r\t	          | j        j                  }t          |g|j        R |j        |dt          |          	                    d                    }n=t          |j        |j        dt          |          	                    d                    }|S )a  Standardized metadata.

        The properties returned depend on the value of both ``index`` and
        ``page``. ``index`` selects a series and ``page`` allows selecting a
        single page from the selected series. If ``index=Ellipsis``, ``page`` is
        understood as a flat index, i.e., the selection ignores individual
        series inside the file. If ``index=Ellipsis`` and ``page=None`` then
        global (file-level) properties are returned. If ``index=Ellipsis``
        and ``page=...``, file-level properties for the flattened index are
        returned.

        Parameters
        ----------
        index : int
            If ``int``, select the ndimage (series) located at that index inside
            the file. If ``Ellipsis`` and ``page`` is ``int`` extract the
            properties of the page located at that (flat) index inside the file.
            If ``Ellipsis`` and ``page=None``, return the properties for the
            batch of all ndimages in the file.
        page : int
            If ``None`` return the properties of the full ndimage. If ``...``
            return the properties of the flattened index. If ``int``,
            return the properties of the page at the selected index only.

        Returns
        -------
        image_properties : ImageProperties
            The standardized metadata (properties) of the selected ndimage or series.

        r   NTr   )shapedtypen_imagesrH   spacingF)rq   rr   rH   rt   )
r?   r(   rb   r6   lenr   rq   rr   r    get)r0   r3   r   page_idxtarget_pagen_seriespropsn_pagess           r   
propertieszTifffilePlugin.properties5  s{   > 
x 00011dH(.2KK(/%06x@KH48?++H#4+"344!'!'4488FF  EE h48#3#3$(.))G#3!233!' '4488FF  EE $!'!''4488FF	  E r<   c                     | j         | j                                          t                                                       d S )N)r(   rM   r&   )r0   r2   s    r   rM   zTifffilePlugin.closex  s2    8HNNr<   .c              +      K   |t           u r| j        j        }n| j        j        |         }|D ]} |j        di |V  dS )a7  Yield pages from a TIFF file.

        This generator walks over the flat index of the pages inside an
        ImageResource and yields them in order.

        Parameters
        ----------
        index : int
            The index of the series to yield pages from. If Ellipsis, walk over
            the file's flat index (and ignore individual series).
        kwargs : Any
            Additional kwargs are passed to TiffPage's ``as_array`` method.

        Yields
        ------
        page : np.ndarray
            A page stored inside the TIFF file.

        Nr8   )r?   r(   rb   r6   rC   )r0   r3   r1   rb   r   s        r   
iter_pageszTifffilePlugin.iter_pages  sf      * HHNEEHOE*E 	) 	)D$,((((((((	) 	)r<   )r   N).)__name__
__module____qualname____doc__r   r'   intr@   ndarrayrE   rB   r   boolr   bytesrJ   r?   r   strr   r   r   r|   rM   r   __classcell__)r2   s   @r   r"   r"   X   s        
I 
Id 
I 
I 
I 
I 
I 
I  $(T 4 4 4S 4s 4
 4 4 4 4l-
 - - - -& 7<&# &# &# &#/3&#	%&# &# &# &#R 'DRVL L LL.1LKOL	c3hL L L L\ *.4 A A A3 AS AO A A A AF     ) ) ) ) ) ) ) )r<   r"   )r   ior   typingr   r   r   r   r   numpyr@   r+   core.requestr	   r
   r   core.v3_plugin_apir   r   r   TiffPager   r    r"   r8   r<   r   <module>r      s  + +Z       , , , , , , , , , , , ,       B B B B B B B B B B : : : : : : : :      (+ S#X    <E) E) E) E) E)X E) E) E) E) E)r<   