
    ⨽g7                         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y)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                 |   i }	 | j                   d   j                  j                  |d<   	 | j                   d   j                  }| j                   d   j                  }|d   |d   z  |d   |d   z  f|d<   |S # t        $ r |cY S w xY w# t        $ r Y |S t        $ r t	        j
                  dt               Y |S w xY w)	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       d/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/imageio/plugins/tifffile_v3.py_get_resolutionr    :   s    H&*iin&:&:&@&@"#

yy~++yy~++ Ol1o-Ol1o-"
 O-     O  
	
 O
s)   &A8 AB	 8BB		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                 d   t         |   |       d | _        |j                  j                  dk(  r+	 t        j                  |j                         fi || _        y t        j                  |j                         fi || _        y # t
        j
                  j                  $ r t        d      w xY w)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-=-=-?J6J  **7+;+;+=HHDH $$22 N)*LMMNs   )B )B/)indexr   r3   r   c                h   d|vr||d<   n|t        d      |d|v rt        d      d|v r|j                  d      }n|nd}|t        u r7|5t        j                   | j
                  di |D cg c]  }| c}      }|S |t        u rdn|} | j                  j                  dd|i|}|S c c}w )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    )
ValueErrorpopEllipsisnpstackiterr(   asarray)r0   r3   r   r1   xndimages         r   readzTifffilePlugin.readw   s    <  F5MKLL V!3OPPJJx(EEHhh9499+>v+>?a?@G
  "X-D5E&dhh&&>e>v>G  @s   1	B/c              +   j   K   | j                   j                  D ]  } |j                  di |  yw)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.
        Nr7   )r(   r6   r>   )r0   r1   sequences      r   r=   zTifffilePlugin.iter   s5       	-H"("",V,,	-s   13F)is_batchr@   rD   c                f   |st        j                  |      dddf   }|D ]  } | j                  j                  |fi | ! | j                  j
                  t        k(  rR| j                  j                          t        t        | j                  j                               }|j                         S y)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;   r>   r(   write_request	_uri_typer	   closer   r   r-   getvalue)r0   r@   rD   r1   imagefiles         r   rF   zTifffilePlugin.write   s    : jj)$'2G 	,EDHHNN5+F+	, ==""i/HHNN!7!7!9:D==?" 0    T)r3   r   exclude_appliedrN   c          
      &   |t         ur)|'| j                  j                  |   j                  |   }nW|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   <   |s't        | j                  |dz         sAt        | j                  |dz         }t        |t              r|j                  |d          |j                  |        |S |j                  }|j                  |j                  D 	ci c]  }	|	j                  |	j                    c}	       |j                  |j"                  |j$                  |j&                  d|j(                  |j*                  |j,                  dt/        |      d|j0                  i       |S c c}	w )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_	_metadata)planar_configurationcompression	predictororientationdescription1descriptionsoftwaredatetime)r:   r(   r6   pagesrP   r+   TIFF
FILE_FLAGSgetattrhasattr
isinstancetupleupdatekeyframer   namer   planarconfigrT   rU   rW   rX   rY   r    rZ   )
r0   r3   r   rN   targetr   flag
flag_valueflavor_metadatatags
             r   r   zTifffilePlugin.metadata   s   >  T%5XX__U+11$7F("t| XX__U+11!4Fh4#3XX^^D)FF>$(HH$6$6H[! 00 	9$TXXut|<
)3&'$((D;4F"G&-dhh{8J&KO!/59 (:; 8	9: # __FOOFKKHSSXXsyy0HIOO,2,?,?#)#5#5!'!1!1#'$*$7$7#)#5#5 &
 &f-
 
  Is   9Hc          	         |xs d}|dt         fv rdn|}|t         u r| j                  j                  |   }n&| j                  j                  |   j                  |   }|t         u rc|at	        | j                  j                        }t        |g|j                  |j                  |dt        |      j                  d            }|S |t         u ri|t         u rat	        | j                  j                        }t        |g|j                  |j                  |dt        |      j                  d            }|S t        |j                  |j                  dt        |      j                  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_imagesrD   spacingF)rl   rm   rD   ro   )
r:   r(   r[   r6   lenr   rl   rm   r    get)r0   r3   r   page_idxtarget_pagen_seriespropsn_pagess           r   
propertieszTifffilePlugin.properties5  s[   > 
x 001dH((..2K((//%066x@KH488??+H#4+"3"34!''!'488FE0 # h48#3$((..)G#3!2!23!'' '488FE  $!''!'''488F	E rM   c                 n    | j                   | j                   j                          t        |           y )N)r(   rI   r&   )r0   r2   s    r   rI   zTifffilePlugin.closex  s$    88HHNNrM   c              +      K   |t         u r| j                  j                  }n| j                  j                  |   }|D ]  } |j                  di |  yw)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.

        Nr7   )r:   r(   r[   r6   r>   )r0   r3   r1   r[   r   s        r   
iter_pageszTifffilePlugin.iter_pages  sR     * HHHNNEHHOOE*E 	)D$,,(((	)s   AA)r   N).)__name__
__module____qualname____doc__r   r'   intr;   ndarrayrA   r=   r   boolr   bytesrF   r:   r   strr   r   r   rw   rI   rz   __classcell__)r2   s   @r   r"   r"   X   s    
I 
Id 
I  $(T 4S 4s 4

 4l-

 -& 7<&# &#/3&#	%&#R 'DRVLL.1LKOL	c3hL\ *.4 A3 AS AO AF)rM   r"   )r~   ior   typingr   r   r   r   r   numpyr;   r+   core.requestr	   r
   r   core.v3_plugin_apir   r   r   TiffPager   r    r"   r7   rM   r   <module>r      sZ   +Z  , ,    B B : (++ S#X <E)X E)rM   