
    9i7                         S r SSKJr  SSKJrJrJrJr  SSKrSSK	r
SSKrSSKJrJrJr  SSKJrJr  SSKJr  S	\R(                  S
\\\4   4S jr " S S\5      rg)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                    0 n U R                   S   R                  R                  US'    U R                   S   R                  nU R                   S   R                  nUS   US   -  US   US   -  4US'   U$ ! [         a    Us $ f = f! [         a     U$ [         a    [        R
                  " S[        5         U$ f = f)	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/land-doc-ocr/venv/lib/python3.13/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                     ^  \ rS rSrSrS\SS4U 4S jjrSSS.S\S	\S\R                  4S
 jjr
S\R                  4S jrSS.S\S\S\\   4S jjr\SSS.S\S	\S\S\\\4   4S jjrSSS.S\S	\S\4S jjrSU 4S jjrSS jrSrU =r$ )TifffilePluginX   a  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                 l  > [         TU ]  U5        S U l        UR                  R                  S:X  a,   [
        R                  " UR                  5       40 UD6U l        g [
        R                  " UR                  5       40 UD6U l        g ! [
        R
                  R                   a    [        S5      ef = f)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(   TifffilePlugin.__init__g   s    !<<3&N#,,W-=-=-?J6J  **7+;+;+=HHDH $$22 N)*LMMNs   *B	 	*B3)indexr   r5   r   c                   SU;  a  X#S'   OUb  [        S5      eUb  SU;   a  [        S5      eSU;   a  UR                  S5      nOUb  OSnU[        L a;  Uc8  [        R                  " U R
                  " S0 UD6 Vs/ s H  oDPM     sn5      nU$ U[        L a  SOUnU R                  R                  " SSU0UD6nU$ s  snf )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)r1   r5   r   r2   xndimages         r   readTifffilePlugin.readw   s    <  5MKLL V!3OPPJJx(EEHhh499+>v+>?+>a+>?@G
  "X-D5Ehh&&>e>v>G  @s   <B=c              +   n   #    U R                   R                   H  nUR                  " S0 UD6v   M     g7f)zYield ndimages from the TIFF.

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

Yields
------
ndimage : np.ndarray
    A decoded ndimage.
Nr9   )r)   r8   r@   )r1   r2   sequences      r   r?   TifffilePlugin.iter   s-      H"",V,, (s   35F)is_batchrB   rH   c                x   U(       d  [         R                  " U5      SSS24   nU H   nU R                  R                  " U40 UD6  M"     U R                  R
                  [        :X  aR  U R                  R                  5         [        [        U R                  R                  5       5      nUR                  5       $ g)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)r1   rB   rH   r2   imagefiles         r   rJ   TifffilePlugin.write   s    : jj)$'2GEHHNN5+F+  ==""i/HHNN!7!7!9:D==?" 0    T)r5   r   exclude_appliedrS   c          
      ^   U[         La*  Ub'  U R                  R                  U   R                  U   nO[U[         La*  Uc'  U R                  R                  U   R                  S   nO(U[         L a  Ub  U R                  R                  U   nOSn0 nUc  U R                  R                  US'   [
        R                  R                   H  n[        U R                  SU-   5      nXuSU-   '   U(       d  M,  [        U R                  US-   5      (       d  ML  [        U R                  US-   5      n[        U[        5      (       a  UR                  US   5        M  UR                  U5        M     U$ UR                  nUR                  UR                   V	s0 s H  oR                  U	R                   _M     sn	5        UR                  UR"                  UR$                  UR&                  SUR(                  UR*                  UR,                  S.[/        U5      ESUR0                  0E5        U$ s  sn	f )a  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)   r8   pagesrU   r,   TIFF
FILE_FLAGSgetattrhasattr
isinstancetupleupdatekeyframer   namer   planarconfigrY   rZ   r\   r]   r^   r    r_   )
r1   r5   r   rS   targetr   flag
flag_valueflavor_metadatatags
             r   r   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$TXXut|<
)3&:'$((D;4F"G"G&-dhh{8J&KO!/599 (:; 8 1: # __FOOFKKHKSXXsyy0KHIOO,2,?,?#)#5#5!'!1!1#'$*$7$7#)#5#5 &
 &f-
 
  Is    H*c          	         U=(       d    SnUS[         4;   a  SOUnU[         L a  U R                  R                  U   nO&U R                  R                  U   R                  U   nU[         L aa  Uc^  [	        U R                  R                  5      n[        U/UR                  Q7UR                  US[        U5      R                  S5      S9nU$ U[         L ag  U[         L a^  [	        U R                  R                  5      n[        U/UR                  Q7UR                  US[        U5      R                  S5      S9nU$ [        UR                  UR                  S[        U5      R                  S5      S9nU$ )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)rr   rs   rH   ru   )
r<   r)   r`   r8   lenr   rr   rs   r    get)r1   r5   r   page_idxtarget_pagen_seriespropsn_pagess           r   
properties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 rR   c                 p   > U R                   b  U R                   R                  5         [        TU ]  5         g )N)r)   rM   r'   )r1   r3   s    r   rM   TifffilePlugin.closex  s$    88HHNNrR   c              +      #    U[         L a  U R                  R                  nOU R                  R                  U   nU H  nUR                  " S0 UD6v   M     g7f)a  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.

Nr9   )r<   r)   r`   r8   r@   )r1   r5   r2   r`   r   s        r   
iter_pagesTifffilePlugin.iter_pages  sJ     * HHHNNEHHOOE*ED,,((( s   AA)r)   )r   N).)__name__
__module____qualname____firstlineno____doc__r   r(   intr=   ndarrayrC   r?   r   boolr   bytesrJ   r<   r   strr   r   r   r}   rM   r   __static_attributes____classcell__)r3   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) )rR   r"   )r   ior   typingr   r   r   r   r   numpyr=   r,   core.requestr	   r
   r   core.v3_plugin_apir   r   r   TiffPager   r    r"   r9   rR   r   <module>r      sZ   +Z  , ,    B B : (++ S#X <E)X E)rR   