
    9iW                         S r SSKrSSKrSSKJr  SSKJrJrJrJ	r	J
r
JrJrJrJr  SSKrSSKJrJrJrJrJr  SSKJr  SSKJrJrJrJr  SS	KJrJ r   SS
KJ!r!  S\\"   4S jr#S\"S\$S\4S jr% " S S\ 5      r&g)a  Read/Write images using Pillow/PIL.

Backend Library: `Pillow <https://pillow.readthedocs.io/en/stable/>`_

Plugin that wraps the the Pillow library. Pillow is a friendly fork of PIL
(Python Image Library) and supports reading and writing of common formats (jpg,
png, gif, tiff, ...). For, the complete list of features and supported formats
please refer to pillows official docs (see the Backend Library link).

Parameters
----------
request : Request
    A request object representing the resource to be operated on.

Methods
-------

.. autosummary::
    :toctree: _plugins/pillow

    PillowPlugin.read
    PillowPlugin.write
    PillowPlugin.iter
    PillowPlugin.get_meta

    N)BytesIO)	AnyCallableDictIteratorListOptionalTupleUnioncast)ExifTagsGifImagePluginImageImageSequenceUnidentifiedImageError)__version__   )	URI_BYTESInitializationErrorIOModeRequest)ImagePropertiesPluginV3)	ArrayLikereturnc                  N    [        S [        R                  " S5       5       5      $ )Nc              3   8   #    U  H  n[        U5      v   M     g 7fN)int).0xs     V/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/imageio/plugins/pillow.py	<genexpr>!pillow_version.<locals>.<genexpr>.   s     8!7AQ!7s   .)tuplepil_versionsplit     r"   pillow_versionr+   -   s    8!2!23!7888r*   orientationmodec           	         ^ [         R                  " U5      S:  a  SOSmS U4S jS U4S jU4S jS	 U4S
 jS S.nUR                  U S 5      $ )N   c                     U $ r   r)   r!   s    r"   <lambda>-_exif_orientation_transform.<locals>.<lambda>9   s    Qr*   c                 .   > [         R                  " U TS9$ )Naxisnpflipr!   r8   s    r"   r4   r5   :   s    RWWQT*r*   c                 ,    [         R                  " U SS9$ )Nr   kr:   rot90r3   s    r"   r4   r5   ;       RXXa1%r*   c                 4   > [         R                  " U TS-
  S9$ )Nr/   r7   r9   r<   s    r"   r4   r5   <   s    RWWQTAX.r*   c                 T   > [         R                  " [         R                  " U SS9TS9$ )N   r>   r7   r:   r;   rA   r<   s    r"   r4   r5   =       RWWRXXa1-D9r*   c                 ,    [         R                  " U SS9$ )NrE   r>   r@   r3   s    r"   r4   r5   >   rB   r*   c                 T   > [         R                  " [         R                  " U SS9TS9$ )Nr/   r>   r7   rF   r<   s    r"   r4   r5   ?   rG   r*   c                 ,    [         R                  " U SS9$ )Nr/   r>   r@   r3   s    r"   r4   r5   @   rB   r*   )r/   r   rE                  c                     U $ r   r)   r3   s    r"   r4   r5   E   s    qr*   )r   getmodebandsget)r,   r-   EXIF_ORIENTATIONr8   s      @r"   _exif_orientation_transformrT   1   sU    
 ##D)A-22D *%.9%9%	 [99r*   c                     ^  \ rS rSrS\SS4U 4S jjrS#S jrSSSSSSSSS	.S
\S\S\	S\	S\	S\S\	S\	S\
R                  4S jjrSSSS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\S\S\	S\\   4
S jjrS rSS.S\\\4   4S jjr S$S
\S\	S\\\4   4S  jjrS%S
\S\4S! jjrS"rU =r$ )&PillowPluginH   requestr   Nc                 v  > [         TU ]  U5         SSKJn  U" 5          SSKJn  U" 5         SU l        / U l        UR                  R                  [        R                  :X  a,   [        R                  " UR                  5       5      nX@l        g0 U l        U R*                  R,                  =(       d    U R*                  R.                  nUc  [0        R2                  " S[4        5        g[        R6                  [        R8                  /nU H3  nU" 5         U[        R:                  " 5       R=                  5       ;   d  M3    g   [%        S	U S
35      Se! [         a     GN?f = f! [         a     GNBf = f! [         a;    UR                   ["        :X  a  [%        S5      Se[%        SUR&                   S35      Sef = f)zInstantiate a new Pillow Plugin Object

Parameters
----------
request : {Request}
    A request object representing the resource to be operated on.

r   )register_heif_opener)register_avif_openerNz'Pillow can not read the provided bytes.zPillow can not read r%   zCan't determine file format to write as. You _must_ set `format` during write or the call will fail. Use `extension` to supress this warning. zPillow can not write `z` files.)super__init__pillow_heifrZ   ImportErrorr[   _imageimages_to_writer-   io_moder   readr   openget_filer   	_uri_typer   r   raw_uri	save_argsrX   	extensionformat_hintwarningswarnUserWarningpreinitinitregistered_extensionskeys)	selfrX   rZ   r[   imageri   tirageformat_loader	__class__s	           r"   r]   PillowPlugin.__init__I   s    	!	#8 !"	#8 !"!!<<6;;.  

7#3#3#56  KDN..J$,,2J2JI <  	 mmUZZ0F!' ; ; = B B DD "(
 &(8<i  		  		 *  $$	1-A  ..w.?qA  s/   E E" $$E3 
EE"
E0/E03AF8c                     U R                  5         U R                  (       a  U R                  R                  5         U R                  R	                  5         g r   )_flush_writerr`   close_requestfinish)rr   s    r"   rz   PillowPlugin.close   s5    ;;KKr*   FT)indexr-   rotateapply_gammawriteable_outputpilmode
exifrotateas_grayr~   r-   r   r   r   r   r   r   c                   Ub  [         R                  " S[        5        UnUb  [         R                  " S[        5        UnUb  [        S5      eU R                  R
                  S:X  a#  [        R                  R                  [        l	        UcD  U R                  R
                  S:X  a  [        nO#U R                  R                  S:X  a  [        nOSn[        U[        5      (       a:  U R                  R                  U5        U R                  U R                  X#XE5      n	U	$ U R!                  UUUUS9n
["        R$                  " U
 Vs/ s H  oPM     snSS9n	U	$ s  snf )	a  
Parses the given URI and creates a ndarray from it.

Parameters
----------
index : int
    If the ImageResource contains multiple ndimages, and index is an
    integer, select the index-th ndimage from among them and return it.
    If index is an ellipsis (...), read all ndimages in the file and
    stack them along a new batch dimension and return them. If index is
    None, this plugin reads the first image of the file (index=0) unless
    the image is a GIF or APNG, in which case all images are read
    (index=...).
mode : str
    Convert the image to the given mode before returning it. If None,
    the mode will be left unchanged. Possible modes can be found at:
    https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes
rotate : bool
    If True and the image contains an EXIF orientation tag,
    apply the orientation before returning the ndimage.
apply_gamma : bool
    If True and the image contains metadata about gamma, apply gamma
    correction to the image.
writable_output : bool
    If True, ensure that the image is writable before returning it to
    the user. This incurs a full copy of the pixel data if the data
    served by pillow is read-only. Consequentially, setting this flag to
    False improves performance for some images.
pilmode : str
    Deprecated, use `mode` instead.
exifrotate : bool
    Deprecated, use `rotate` instead.
as_gray : bool
    Deprecated. Exists to raise a constructive error message.

Returns
-------
ndimage : ndarray
    A numpy array containing the loaded image data

Notes
-----
If you read a paletted image (e.g. GIF) then the plugin will apply the
palette by default. Should you wish to read the palette indices of each
pixel use ``mode="P"``. The coresponding color pallete can be found in
the image's metadata using the ``palette`` key when metadata is
extracted using the ``exclude_applied=False`` kwarg. The latter is
needed, as palettes are applied by default and hence excluded by default
to keep metadata and pixel data consistent.

z,`pilmode` is deprecated. Use `mode` instead.z1`exifrotate` is deprecated. Use `rotate` instead.zThe keyword `as_gray` is no longer supported.Use `mode='F'` for a backward-compatible result, or  `mode='L'` for an integer-valued result.GIF
image/apngr   r-   r   r   r   r7   )rk   rl   DeprecationWarning	TypeErrorr`   formatr   LoadingStrategy RGB_AFTER_DIFFERENT_PALETTE_ONLYLOADING_STRATEGYEllipsiscustom_mimetype
isinstancer   seek_apply_transformsiterr:   stack)rr   r~   r-   r   r   r   r   r   r   rs   iteratorims               r"   rc   PillowPlugin.read   sO   @ MM>@R D!MMCEW  F<  ;;& ..OO + ={{!!U* ,,< eS!!KKU#**T;E  yy'!1	 ! H HH848Rb841=E 5s   Er   c             #      #    [         R                  " U R                  5       H  nU R                  XQX#U5      v   M     g7f)aO  
Iterate over all ndimages/frames in the URI

Parameters
----------
mode : {str, None}
    Convert the image to the given mode before returning it. If None,
    the mode will be left unchanged. Possible modes can be found at:
    https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes
rotate : {bool}
    If set to ``True`` and the image contains an EXIF orientation tag,
    apply the orientation before returning the ndimage.
apply_gamma : {bool}
    If ``True`` and the image contains metadata about gamma, apply gamma
    correction to the image.
writable_output : bool
    If True, ensure that the image is writable before returning it to
    the user. This incurs a full copy of the pixel data if the data
    served by pillow is read-only. Consequentially, setting this flag to
    False improves performance for some images.
N)r   r   r`   r   )rr   r-   r   r   r   r   s         r"   r   PillowPlugin.iter  s;     <  ((5B((&/?  6s   >A c                    Ub  UR                  U5      nOUR                  S:X  a&  UR                  UR                  R                  5      nOUR                  S:X  av  UR                  S:X  af  [	        5       u  pgn[
        R                  S:X  a  Sn	OSn	US:  a  [        R                  " S[        5        OUS	:  a  Xl        OUR                  U	5      n[        R                  " U5      nU R                  U R                  R                  5       S
S9n
U(       a1  SU
;   a+  [        U
S   U R                  R                  5      nU" U5      nU(       a  SU
;   a~  [!        U
S   5      n[!        UR"                  [        R$                  :X  a  SOS5      nSnX-  U-  U-  U-  S-   n[        R&                  " U5      R)                  [        R*                  5      nU(       a*  UR,                  S   (       d  [        R.                  " U5      nU$ )NPPNGIlittlezI;16zI;16B
   zLoading 16-bit (uint16) PNG as int32 due to limitations in pillow's PNG decoder. This will be fixed in a future version of pillow which will make this warning dissapear.r/   Fr~   exclude_appliedOrientationgammai      g      ?g<Nё\?	WRITEABLE)convertr-   paletter   r+   sys	byteorderrk   rl   rm   r:   asarraymetadatar`   tellrT   floatdtypeuint16roundastypeuint8flagsarray)rr   rs   r-   r   r   r   majorminorpatchdesired_modemetatransformationr   scalegains                  r"   r   PillowPlugin._apply_transforms.  s    MM$'EZZ3 MM%--"4"45E\\U"uzzS'8"0"2E%}}(%  'rzP  	 )
 l3

5!}}4;;#3#3#5u}Mmt+8]#T[[%5%5N #5)E7d?$w-(E5;;"))#;%EEDm-6=FEHHUO**2884EEKK$<HHUOEr*   )r-   r   is_batchndimager   r   c                   SU;   a2  [         R                  " S[        5        SUR                  S5      -  US'   [	        U[
        5      (       a  [        R                  " USS9nSnO[        R                  " U5      nUb  OUb9  [        R                  " U5      S	:  a  UR                  S
:  OUR                  S:  nOiUR                  S:X  a  SnOVUR                  S
:X  a  UR                  S   S	:X  a  [        S5      eUR                  S
:X  a  UR                  S   S;   a  SnOSnU(       d  US   nU HM  n[        R                  " XbS9nSU;   a  UR                  SUS   -  S9nU R                   R#                  U5        MO     UbT  SU R$                  ;   aD  U R$                  S   U:w  a1  U R$                  S   n[         R                  " SU SU S3[&        5        U R(                  R*                  =(       d    U R(                  R,                  n	U=(       d    [        R.                  " 5       U	   U R$                  S'   U R$                  R1                  U5        Sn
U R2                  R4                  [6        :X  aH  U R9                  5         [;        [<        U R2                  R?                  5       5      nURA                  5       n
U
$ )a  
Write an ndimage to the URI specified in path.

If the URI points to a file on the current host and the file does not
yet exist it will be created. If the file exists already, it will be
appended if possible; otherwise, it will be replaced.

If necessary, the image is broken down along the leading dimension to
fit into individual frames of the chosen format. If the format doesn't
support multiple frames, and IOError is raised.

Parameters
----------
image : ndarray or list
    The ndimage to write. If a list is given each element is expected to
    be an ndimage.
mode : str
    Specify the image's color format. If None (default), the mode is
    inferred from the array's shape and dtype. Possible modes can be
    found at:
    https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes
format : str
    Optional format override. If omitted, the format to use is
    determined from the filename extension. If a file object was used
    instead of a filename, this parameter must always be used.
is_batch : bool
    Explicitly tell the writer that ``image`` is a batch of images
    (True) or not (False). If None, the writer will guess this from the
    provided ``mode`` or ``image.shape``. While the latter often works,
    it may cause problems for small images due to aliasing of spatial
    and color-channel axes.
kwargs : ...
    Extra arguments to pass to pillow. If a writer doesn't recognise an
    option, it is silently ignored. The available options are described
    in pillow's `image format documentation
    <https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html>`_
    for each writer.

Notes
-----
When writing batches of very narrow (2-4 pixels wide) gray images set
the ``mode`` explicitly to avoid the batch being identified as a colored
image.

fpszvThe keyword `fps` is no longer supported. Use `duration`(in ms) instead, e.g. `fps=50` == `duration=20` (1000 * 1/50).i  durationr   r7   TNr/   rE   r   Fr1   z*Can't write images with one color channel.)r   rE   rK   )N.)r-   bits)colorsr   zSChanging the output format during incremental writes is strongly discouraged. Was `z`, is now `z`.)!rk   rl   r   rR   r   listr:   r   r   r   rQ   ndimshape
ValueError	fromarrayquantizera   appendrh   rm   rX   ri   rj   rp   updater{   rf   r   ry   r   r   re   getvalue)rr   r   r-   r   r   kwargsframe	pil_frame
old_formatri   resultfiles               r"   writePillowPlugin.writec  se   l F?MMQ"
 "*FJJu,=!=F:gt$$hhwQ/GHjj)G $)$6$6t$<q$@q gllUVFV  \\QH\\Q7==#4#9IJJ\\Q7==#4	#AHHi(GE9I%..a6&>6I.J	  ''	2	  DNN*x(F21JMM#Kxr; 	 LL**Fdll.F.F	#)#UU-H-H-J9-Ux f% ==""i/ !7!7!9:D]]_Fr*   c                    [        U R                  5      S:X  a  g U R                  R                  S5      n[        U R                  5      S:  a(  SU R                  S'   U R                  U R                  S'   UR                  " U R
                  R                  5       40 U R                  D6  U R                  R                  5         U R                  R                  5         g )Nr   Tsave_allappend_images)lenra   poprh   saver{   re   clear)rr   primary_images     r"   ry   PillowPlugin._flush_writer  s    t##$),,003t##$q()-DNN:&.2.B.BDNN?+4==113Ft~~F""$r*   r   )r~   c                "    U R                  USS9$ )NFr   )r   )rr   r~   s     r"   get_metaPillowPlugin.get_meta  s    }}5%}@@r*   r   c                 D   UcD  U R                   R                  S:X  a  [        nO#U R                   R                  S:X  a  [        nOSn[	        U[
        5      (       a9  U R                   R                  5       U:w  a  U R                   R                  U5        U R                   R                  R                  5       nU R                   R                  US'   U R                   R                  US'   U R                   R                  S:X  aU  U(       dN  [        R                  " [        U R                   R                  R                   R#                  5       5      5      US'   U R                   R%                  5       (       a  ['        U R                   R%                  5       5      R)                  5        VVs0 s H&  u  pE[*        R,                  R/                  US	5      U_M(     nnnUR1                  S	S5        UR3                  U5        U(       a  UR1                  S
S5        U$ s  snnf )aB  Read ndimage metadata.

Parameters
----------
index : {integer, None}
    If the ImageResource contains multiple ndimages, and index is an
    integer, select the index-th ndimage from among them and return its
    metadata. If index is an ellipsis (...), read and return global
    metadata. If index is None, this plugin reads metadata from the
    first image of the file (index=0) unless the image is a GIF or APNG,
    in which case global metadata is read (index=...).
exclude_applied : bool
    If True, exclude metadata fields that are applied to the image while
    reading. For example, if the binary data contains a rotation flag,
    the image is rotated by default and the rotation flag is excluded
    from the metadata to avoid confusion.

Returns
-------
metadata : dict
    A dictionary of format-specific metadata.

Nr   r   r   r-   r   r   r   unknownr   )r`   r   r   r   r   r   r   r   infocopyr-   sizer:   r   r&   r   r   rq   getexifdictitemsr   TAGSrR   r   r   )rr   r~   r   r   keyvalue	exif_datas          r"   r   PillowPlugin.metadata  s   6 ={{!!U* ,,< eS!!dkk&6&6&8E&AKKU#;;##((*;;++ KK,,;;s"?"$**U4;;3F3F3M3M3R3R3T-U"VHY;;   #'t{{':':'<"="C"C"E"EJC !!#y158"E   MM)T*OOI&LL-s   .-Hc                 0   UcD  U R                   R                  S:X  a  [        nO#U R                   R                  S:X  a  [        nOSnU[        L a  U R                   R	                  S5        OU R                   R	                  U5        U R                   R
                  S:X  a!  U R                   R                  R
                  nOU R                   R
                  nU R                   R                  nU R                   R                  nXC4nSnUSL a  [        U R                   SS5      nU/UQ7n[        R                  " [        R                  " US	5      5      nUR                  n[        U5      S
:  a
  / UQUS
S Q7n[!        UUR"                  UU[        L S9$ )a  Standardized ndimage metadata
Parameters
----------
index : int
    If the ImageResource contains multiple ndimages, and index is an
    integer, select the index-th ndimage from among them and return its
    properties. If index is an ellipsis (...), read and return the
    properties of all ndimages in the file stacked along a new batch
    dimension. If index is None, this plugin reads and returns the
    properties of the first image (index=0) unless the image is a GIF or
    APNG, in which case it reads and returns the properties all images
    (index=...).

Returns
-------
properties : ImageProperties
    A dataclass filled with standardized image metadata.

Notes
-----
This does not decode pixel data and is fast for large images.

Nr   r   r   r   .n_framesr/   )r/   r/   r   )r   r   n_imagesr   )r`   r   r   r   r   r-   r   widthheightgetattrr:   r   r   newr   r   r   r   )	rr   r~   r-   r   r   r   r   dummy	pil_shapes	            r"   
propertiesPillowPlugin.properties'  sS   2 ={{!!U* ,,< HKKQKKU#;;s";;&&++D;;##D[[&&kk(("("&C<t{{J:H&&E

599T623%*[[	y>A,e,im,E++h&	
 	
r*   )r`   ra   rh   )r   N)NTr   )__name__
__module____qualname____firstlineno__r   r]   rz   r   strboolr:   ndarrayrc   r   r   r   r   r   r   r	   bytesr   ry   r   r   r   r   r   r   __static_attributes____classcell__)rv   s   @r"   rV   rV   H   s   E ED EN !!%q q 	q
 q q q q q q 
ql !!%! ! 	!
 ! ! 
"**	!F3	3r wy$y/12w 	w
 w w 
%wr !" Ad38n A :>88268	c3h8t?
 ?
 ?
 ?
r*   rV   )'__doc__r   rk   ior   typingr   r   r   r   r   r	   r
   r   r   numpyr:   PILr   r   r   r   r   r   r'   core.requestr   r   r   r   core.v3_plugin_apir   r   r   r   r+   r   rT   rV   r)   r*   r"   <module>r     sn   6    T T T  V V * J J : 9c
 9:S : : :.^
8 ^
r*   