
    0-PhP                         d Z ddlZddlmZ ddlmZmZ ddlZddl	Z		 ddl
Zn"# e$ r  e	j        de           ddlmZ Y nw xY wd	Zd
ZdZ G d de          ZdS )a   Read/Write TIFF files.

Backend: internal

Provides support for a wide range of Tiff images using the tifffile
backend.

Parameters for reading
----------------------
offset : int
    Optional start position of embedded file. By default this is
    the current file position.
size : int
    Optional size of embedded file. By default this is the number
    of bytes from the 'offset' to the end of the file.
multifile : bool
    If True (default), series may include pages from multiple files.
    Currently applies to OME-TIFF only.
multifile_close : bool
    If True (default), keep the handles of other files in multifile
    series closed. This is inefficient when few files refer to
    many pages. If False, the C runtime may run out of resources.

Parameters for saving
---------------------
bigtiff : bool
    If True, the BigTIFF format is used.
byteorder : {'<', '>'}
    The endianness of the data in the file.
    By default this is the system's native byte order.
software : str
    Name of the software used to create the image.
    Saved with the first page only.

Metadata for reading
--------------------
planar_configuration : {'contig', 'planar'}
    Specifies if samples are stored contiguous or in separate planes.
    By default this setting is inferred from the data shape.
    'contig': last dimension contains samples.
    'planar': third last dimension contains samples.
resolution_unit : int
    The resolution unit stored in the TIFF tag. Usually 1 means no/unknown unit,
    2 means dpi (inch), 3 means dpc (centimeter).
resolution : (float, float, str)
    A tuple formatted as (X_resolution, Y_resolution, unit). The unit is a
    string representing one of the following units::

        NONE        # No unit or unit unknown
        INCH        # dpi
        CENTIMETER  # cpi
        MILLIMETER
        MICROMETER

compression : int
    Value indicating the compression algorithm used, e.g. 5 is LZW,
    7 is JPEG, 8 is deflate.
    If 1, data are uncompressed.
predictor : int
    Value 2 indicates horizontal differencing was used before compression,
    while 3 indicates floating point horizontal differencing.
    If 1, no prediction scheme was used before compression.
orientation : {'top_left', 'bottom_right', ...}
    Oriented of image array.
is_rgb : bool
    True if page contains a RGB image.
is_contig : bool
    True if page contains a contiguous image.
is_tiled : bool
    True if page contains tiled image.
is_palette : bool
    True if page contains a palette-colored image and not OME or STK.
is_reduced : bool
    True if page is a reduced image of another image.
is_shaped : bool
    True if page contains shape in image_description tag.
is_fluoview : bool
    True if page contains FluoView MM_STAMP tag.
is_nih : bool
    True if page contains NIH image header.
is_micromanager : bool
    True if page contains Micro-Manager metadata.
is_ome : bool
    True if page contains OME-XML in image_description tag.
is_sgi : bool
    True if page contains SGI image and tile depth tags.
is_mdgel : bool
    True if page contains md_file_tag tag.
is_mediacy : bool
    True if page contains Media Cybernetics Id tag.
is_stk : bool
    True if page contains UIC2Tag tag.
is_lsm : bool
    True if page contains LSM CZ_LSM_INFO tag.
description : str
    Image description
description1 : str
    Additional description
is_imagej : None or str
    ImageJ metadata
software : str
    Software used to create the TIFF file
datetime : datetime.datetime
    Creation date and time

Metadata for writing
--------------------
photometric : {'minisblack', 'miniswhite', 'rgb'}
    The color space of the image data.
    By default this setting is inferred from the data shape.
planarconfig : {'contig', 'planar'}
    Specifies if samples are stored contiguous or in separate planes.
    By default this setting is inferred from the data shape.
    'contig': last dimension contains samples.
    'planar': third last dimension contains samples.
resolution : (float, float) or ((int, int), (int, int))
    X and Y resolution in dots per inch as float or rational numbers.
description : str
    The subject of the image. Saved with the first page only.
compress : int
    Values from 0 to 9 controlling the level of zlib (deflate) compression.
    If 0, data are written uncompressed (default).
compression : str, (int, int)
    Compression scheme used while writing the image. If omitted (default) the
    image is not uncompressed. Compression cannot be used to write contiguous
    series. Compressors may require certain data shapes, types or value ranges.
    For example, JPEG compression requires grayscale or RGB(A), uint8 or 12-bit
    uint16. JPEG compression is experimental. JPEG markers and TIFF tags may not
    match. Only a limited set of compression schemes are implemented. 'ZLIB' is
    short for ADOBE_DEFLATE. The value is written to the Compression tag.
compressionargs:
    Extra arguments passed to compression codec, e.g., compression level. Refer
    to the Imagecodecs implementation for supported arguments.
predictor : bool
    If True, horizontal differencing is applied before compression.
    Note that using an int literal 1 actually means no prediction scheme
    will be used.
volume : bool
    If True, volume data are stored in one tile (if applicable) using
    the SGI image_depth and tile_depth tags.
    Image width and depth must be multiple of 16.
    Few software can read this format, e.g. MeVisLab.
writeshape : bool
    If True, write the data shape to the image_description tag
    if necessary and no other description is given.
extratags: sequence of tuples
    Additional tags as [(code, dtype, count, value, writeonce)].

    code : int
        The TIFF tag Id.
    dtype : str
        Data type of items in 'value' in Python struct format.
        One of B, s, H, I, 2I, b, h, i, f, d, Q, or q.
    count : int
        Number of data values. Not used for string values.
    value : sequence
        'Count' values compatible with 'dtype'.
    writeonce : bool
        If True, the tag is written to the first page only.

Notes
-----
Global metadata is stored with the first frame in a TIFF file.
Thus calling :py:meth:`Format.Writer.set_meta_data` after the first frame
was written has no effect. Also, global metadata is ignored if metadata is
provided via the `meta` argument of :py:meth:`Format.Writer.append_data`.

If you have installed tifffile as a Python package, imageio will attempt
to use that as backend instead of the bundled backend. Doing so can
provide access to new performance improvements and bug fixes.

    N   )Format)	URI_BYTESURI_FILEzImageIO's vendored tifffile backend is deprecated and will be removed in ImageIO v3. Install the tifffile directly: `pip install imageio[tifffile]`   )	_tifffile)z.tifz.tiffz.stkz.lsm)photometricplanarconfig
resolutiondescriptioncompresscompressioncompressionargs	predictorvolume
writeshape	extratagsdatetime)planar_configurationis_fluoviewis_nih	is_contigis_micromanageris_omeis_lsm
is_palette
is_reducedis_rgbis_sgi	is_shapedis_stkis_tiledis_mdgelresolution_unitr   r   
is_mediacyorientationr   description1	is_imagejsoftwarec                   j    e Zd ZdZd Zd Z G d dej                  Z G d dej                  ZdS )	
TiffFormata  Provides support for a wide range of Tiff images using the tifffile
    backend.

    Images that contain multiple pages can be read using ``imageio.mimread()``
    to read the individual pages, or ``imageio.volread()`` to obtain a
    single (higher dimensional) array.

    Note that global metadata is stored with the first frame in a TIFF file.
    Thus calling :py:meth:`Format.Writer.set_meta_data` after the first frame
    was written has no effect. Also, global metadata is ignored if metadata is
    provided via the `meta` argument of :py:meth:`Format.Writer.append_data`.

    If you have installed tifffile as a Python package, imageio will attempt
    to use that as backend instead of the bundled backend. Doing so can
    provide access to new performance improvements and bug fixes.

    Parameters for reading
    ----------------------
    offset : int
        Optional start position of embedded file. By default this is
        the current file position.
    size : int
        Optional size of embedded file. By default this is the number
        of bytes from the 'offset' to the end of the file.
    multifile : bool
        If True (default), series may include pages from multiple files.
        Currently applies to OME-TIFF only.
    multifile_close : bool
        If True (default), keep the handles of other files in multifile
        series closed. This is inefficient when few files refer to
        many pages. If False, the C runtime may run out of resources.

    Parameters for saving
    ---------------------
    bigtiff : bool
        If True, the BigTIFF format is used.
    byteorder : {'<', '>'}
        The endianness of the data in the file.
        By default this is the system's native byte order.
    software : str
        Name of the software used to create the image.
        Saved with the first page only.

    Metadata for reading
    --------------------
    planar_configuration : {'contig', 'planar'}
        Specifies if samples are stored contiguous or in separate planes.
        By default this setting is inferred from the data shape.
        'contig': last dimension contains samples.
        'planar': third last dimension contains samples.
    resolution_unit : (float, float) or ((int, int), (int, int))
        X and Y resolution in dots per inch as float or rational numbers.
    compression : int
        Value indicating the compression algorithm used, e.g. 5 is LZW,
        7 is JPEG, 8 is deflate.
        If 1, data are uncompressed.
    predictor : int
        Value 2 indicates horizontal differencing was used before compression,
        while 3 indicates floating point horizontal differencing.
        If 1, no prediction scheme was used before compression.
    orientation : {'top_left', 'bottom_right', ...}
        Oriented of image array.
    is_rgb : bool
        True if page contains a RGB image.
    is_contig : bool
        True if page contains a contiguous image.
    is_tiled : bool
        True if page contains tiled image.
    is_palette : bool
        True if page contains a palette-colored image and not OME or STK.
    is_reduced : bool
        True if page is a reduced image of another image.
    is_shaped : bool
        True if page contains shape in image_description tag.
    is_fluoview : bool
        True if page contains FluoView MM_STAMP tag.
    is_nih : bool
        True if page contains NIH image header.
    is_micromanager : bool
        True if page contains Micro-Manager metadata.
    is_ome : bool
        True if page contains OME-XML in image_description tag.
    is_sgi : bool
        True if page contains SGI image and tile depth tags.
    is_stk : bool
        True if page contains UIC2Tag tag.
    is_mdgel : bool
        True if page contains md_file_tag tag.
    is_mediacy : bool
        True if page contains Media Cybernetics Id tag.
    is_stk : bool
        True if page contains UIC2Tag tag.
    is_lsm : bool
        True if page contains LSM CZ_LSM_INFO tag.
    description : str
        Image description
    description1 : str
        Additional description
    is_imagej : None or str
        ImageJ metadata
    software : str
        Software used to create the TIFF file
    datetime : datetime.datetime
        Creation date and time

    Metadata for writing
    --------------------
    photometric : {'minisblack', 'miniswhite', 'rgb'}
        The color space of the image data.
        By default this setting is inferred from the data shape.
    planarconfig : {'contig', 'planar'}
        Specifies if samples are stored contiguous or in separate planes.
        By default this setting is inferred from the data shape.
        'contig': last dimension contains samples.
        'planar': third last dimension contains samples.
    resolution : (float, float) or ((int, int), (int, int))
        X and Y resolution in dots per inch as float or rational numbers.
    description : str
        The subject of the image. Saved with the first page only.
    compress : int
        Values from 0 to 9 controlling the level of zlib (deflate) compression.
        If 0, data are written uncompressed (default).
    predictor : bool
        If True, horizontal differencing is applied before compression.
        Note that using an int literal 1 actually means no prediction scheme
        will be used.
    volume : bool
        If True, volume data are stored in one tile (if applicable) using
        the SGI image_depth and tile_depth tags.
        Image width and depth must be multiple of 16.
        Few software can read this format, e.g. MeVisLab.
    writeshape : bool
        If True, write the data shape to the image_description tag
        if necessary and no other description is given.
    extratags: sequence of tuples
        Additional tags as [(code, dtype, count, value, writeonce)].

        code : int
            The TIFF tag Id.
        dtype : str
            Data type of items in 'value' in Python struct format.
            One of B, s, H, I, 2I, b, h, i, f, d, Q, or q.
        count : int
            Number of data values. Not used for string values.
        value : sequence
            'Count' values compatible with 'dtype'.
        writeonce : bool
            If True, the tag is written to the first page only.
    c                    	 t          j        |                                fi |j         np# t          $ r+ Y |                                                    d           dS t           j        $ r+ Y |                                                    d           dS w xY w	 |                                                    d           n,# |                                                    d           w xY wdS )Nr   FT)r   TiffFileget_filekwargs
ValueErrorseekTiffFileErrorselfrequests     X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/tifffile.py	_can_readzTiffFormat._can_read  s   		'w//11DDW^DDDD 	 	 	
 ##A&&&&&	 & 	 	 	##A&&&&&			 E ##A&&&&G##A&&&&ts1   ), C 
BC  B.C BC )C.c                 2   |j         t          t          fv rn|j        | j        vrdS 	 t          j        |                                fi |j         np# t          $ r+ Y |                                
                    d           dS t
          j        $ r+ Y |                                
                    d           dS w xY w	 |                                
                    d           n,# |                                
                    d           w xY wdS )NFr   T)	_uri_typer   r   	extension
extensionsr   
TiffWriterr.   r/   r0   r1   r2   r3   s     r6   
_can_writezTiffFormat._can_write  s8   9 555do555		' !1!1!3!3FFw~FFFF 	 	 	
 ##A&&&&&	 & 	 	 	##A&&&&&			 G ##A&&&&G##A&&&&ts5   )A C+ 
B?C+ B?C+ >B??C+ +)Dc                   ,    e Zd Zd Zd Zd Zd Zd ZdS )TiffFormat.Readerc                    | j         j                            d          r/t          | j                                         d          x| _        }n d | _        | j                                         }t          j        |fi || _	        d S )N)zhttp://zhttps://rb)
r5   filename
startswithopenget_local_filename_fr.   r   r-   _tf)r4   r/   fs      r6   _openzTiffFormat.Reader._open  s|    |$//0GHH ,"4<#B#B#D#DdKKK!!L))++ )!66v66DHHH    c                 |    | j                                          | j        | j                                         d S d S N)rG   closerF   r4   s    r6   _closezTiffFormat.Reader._close  s7    HNNw" #"rJ   c                 4    t          | j        j                  S rL   )lenrG   seriesrN   s    r6   _get_lengthzTiffFormat.Reader._get_length  s    tx'''rJ   c                     |dk     s||                                  k    rt          d          | j                            |          }|                     |          }||fS )Nr   z/Index out of range while reading from tiff file)rR   )rS   
IndexErrorrG   asarray_get_meta_data)r4   indeximmetas       r6   	_get_datazTiffFormat.Reader._get_data  sd    qyyET%5%5%7%777 !RSSS!!!//B&&u--Dt8OrJ   c                    i }| j         j        |pd         }t          D ]&}	 t          ||          ||<   # t          $ r Y #w xY wdD ]G}	 t
          j                            |j        |         j        d          |d<    n# t          $ r Y Dw xY wd|j        v r|j        d         j        j        |d<   d|j        v rd|j        v rd|j        v r|j        d         j        }|j        d         j        }|d	         dk    s|d	         dk    rt          j
        d
t                     n;|d         |d	         z  |d         |d	         z  |j        d         j        j        f|d<   |S )Nr   )r   DateTimez%Y:%m:%d %H:%M:%Sr   i(  r$   i  i  r   zQIgnoring resolution metadata, because at least one direction has a 0 denominator.r   )rG   pagesREAD_METADATA_KEYSgetattr	Exceptionr   strptimetagsvaluewarningswarnRuntimeWarningname)r4   rX   rZ   pagekeyresolution_xresolution_ys          r6   rW   z TiffFormat.Reader._get_meta_data  s   D8>%*1-D)   'c 2 2DII    D 0  '/'8'A'A	#,.A( (D$ E    D di*.)C.*>*D&'diC49$4$4	9I9I#y~3#y~3?a''<?a+?+?MN&    %Q,q/9$Q,q/9	#,1*D& Ks!   5
AA
3A??
BBN)__name__
__module____qualname__rI   rO   rS   r[   rW    rJ   r6   Readerr?     s_        	7 	7 	7	  	  	 
	( 	( 	(	 	 	&	 &	 &	 &	 &	rJ   rq   c                   >    e Zd ZddZd Zd Zed             Zd ZdS )TiffFormat.WriterNc                 0   	 t          j        | j                                        |||          | _        d | _        nJ# t          $ r= t          j        | j                                        ||          | _        || _        Y nw xY wi | _        d| _        d S )N)bigtiff	byteorderr)   )ru   rv   r   )	r   r<   r5   r.   rG   	_software	TypeError_meta_frames_written)r4   ru   rv   r)   s       r6   rI   zTiffFormat.Writer._open  s    *$/L))++#'%	   "& * * * %/L))++W	   "** DJ#$D   s   ;> ABBc                 8    | j                                          d S rL   )rG   rM   rN   s    r6   rO   zTiffFormat.Writer._close  s    HNNrJ   c                 D   ||                      |          }n| j        dk    r| j        ni }| j        | j        dk    r
| j        |d<   	 | j        j        }n# t          $ r | j        j        }Y nw xY w |t          j	        |          fddi| | xj        dz  c_        d S )Nr   r)   
contiguousFr   )
_sanitize_metarz   ry   rw   rG   writeAttributeErrorsavenp
asanyarray)r4   rY   rZ   
write_meths       r6   _append_datazTiffFormat.Writer._append_data  s    **400 &*%9Q%>%>tzzB~)d.Ba.G.G#'>Z +!X^

! + + +!X]


+Jr}R((CCUCdCCC  A%    s   
A A0/A0c                 :   i }|                                  D ]\  }}|t          v ru|dk    rt          |t                    s
|dk    ||<   3|dk    rE|dk    r?t	          j        dt                     t          j        dk     rd|f|d<   qd	|d<   d
|i|d<   ~|||<   |S )Nr   r   r   r   zUThe use of `compress` is deprecated. Use `compression` and `compressionargs` instead.2022   r   zliblevelr   )	itemsWRITE_METADATA_KEYS
isinstanceboolre   rf   DeprecationWarningr   __version__)rZ   retrj   rd   s       r6   r~   z TiffFormat.Writer._sanitize_meta  s    C"jjll ) )
U--- k))*UD2I2I)#(19C
**uzz s.  
 %069923UC..17C.6=u5EC 122#(CJrJ   c                 :    |                      |          | _        d S rL   )r~   ry   )r4   rZ   s     r6   set_meta_datazTiffFormat.Writer.set_meta_data0  s    ,,T22DJJJrJ   )NNN)	rm   rn   ro   rI   rO   r   staticmethodr~   r   rp   rJ   r6   Writerrs     sp        	% 	% 	% 	%(	 	 		& 	& 	&$ 
	 	 
	.	3 	3 	3 	3 	3rJ   r   N)	rm   rn   ro   __doc__r7   r=   r   rq   r   rp   rJ   r6   r+   r+      s        T Tl    (A A A A A A A AHC3 C3 C3 C3 C3 C3 C3 C3 C3 C3rJ   r+   )r   r   corer   core.requestr   r   numpyr   re   tifffiler   ImportErrorrf   r    TIFF_FORMATSr   r_   r+   rp   rJ   r6   <module>r      s  k kZ        . . . . . . . .     	        HM	+ 		    1  8@3 @3 @3 @3 @3 @3 @3 @3 @3 @3s   # AA