Ë
    úmxiÈ>  ã                  óô  — U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZmZ d dlmZmZ d dlmZ d d	lmZ er(d d
lmZ d dlmZ d dlmZmZmZ d dl m!Z! d dl"m#Z$ d dl%m&Z& ed   Z'de(d<   ee'dejR                  e*e
e+f   Z,de(d<   ed   Z-de(d<   ed   Z.de(d<   ee.df   Z/de(d<   e,ee,   z  Z0de(d<   dZ1de(d<    G d„ de«      Z2d e2jf                  _4        d!e2jj                  _4        d"e2jl                  _4        d0d#„Z7d0d$„Z8	 	 	 	 	 	 d1d%„Z9	 	 d2	 	 	 	 	 	 	 d3d&„Z:d4d'„Z;d5d6d(„Z<d7d)„Z=d8d*„Z>	 	 	 	 	 	 	 	 d9d+„Z?d:d,„Z@	 	 	 	 	 	 	 	 	 	 	 	 	 	 d;d-„ZAd<d.„ZB	 	 d=	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d>d/„ZCy)?é    )ÚannotationsN)ÚSequence)ÚIntEnum)ÚPath)ÚTYPE_CHECKINGÚFinalÚLiteralÚ	TypeAliasÚUnionÚcast)ÚruntimeÚurl_util)ÚStreamlitAPIException)Úcaching)ÚAny)ÚGifImagePluginÚImageÚ	ImageFile)ÚLayoutConfig)Ú	ImageList)Ú
NumpyShape)zImageFile.ImageFilezImage.ImagezGifImagePlugin.GifImageFiler
   ÚPILImageúnpt.NDArray[Any]ÚAtomicImage)ÚRGBÚBGRÚChannels)ÚJPEGÚPNGÚGIFÚImageFormatÚautoÚImageFormatOrAutoÚImageOrImageListi´  z
Final[int]ÚMAXIMUM_CONTENT_WIDTHc                  ó$   — e Zd ZdZdZdZdZdZdZy)ÚWidthBehaviorz|
    Special values that are recognized by the frontend and allow us to change the
    behavior of the displayed image.
    éÿÿÿÿéþÿÿÿéýÿÿÿéüÿÿÿéûÿÿÿN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚORIGINALÚCOLUMNÚAUTOÚMIN_IMAGE_OR_CONTAINERÚMAX_IMAGE_OR_CONTAINER© ó    úY/home/htdocs/ttos/venv/lib/python3.12/site-packages/streamlit/elements/lib/image_utils.pyr'   r'   <   s$   „ ñð
 €HØ€FØ€DØÐØÑr7   r'   z'Display the image at its original widthz5Display the image at the width of the column it's in.z‚Display the image at its original width, unless it
would exceed the width of its column in which case clamp it to
its column widthc                ó   — | j                   dv S )N)ÚRGBAÚLAÚP)Úmode©Úimages    r8   Ú_image_may_have_alpha_channelr@   R   s   € Ø�:‰:Ð,Ð,Ð,r7   c                ó    — | j                   dk(  S )Nr    )Úformatr>   s    r8   Ú_image_is_gifrC   V   s   € Ø�<‰<˜5Ñ Ð r7   c                óü   — |j                  «       }|dv rt        d|«      S |dk(  ryt        | t        «      r,ddlm}  |j                  t        j                  | «      «      }n| }t        |«      ryt        |«      ryy)	ap  Return either "JPEG", "PNG", or "GIF", based on the input `format` string.
    - If `format` is "JPEG" or "JPG" (or any capitalization thereof), return "JPEG"
    - If `format` is "PNG" (or any capitalization thereof), return "PNG"
    - For all other strings, return "PNG" if the image has an alpha channel,
    "GIF" if the image is a GIF, and "JPEG" otherwise.
    >   r   r   r!   ÚJPGr   r   ©r   r    r   )Úupperr   Ú
isinstanceÚbytesÚPILr   ÚopenÚioÚBytesIOrC   r@   )Ú
image_datarB   Ú
img_formatr   Ú	pil_images        r8   Ú_validate_image_format_stringrQ   Z   sw   € ð —‘“€JØ�_Ñ$Ü�M :Ó.Ð.ð �UÒØô �*œeÔ$Ýà�E—J‘JœrŸz™z¨*Ó5Ó6‰	àˆ	ä�YÔØä$ YÔ/Øàr7   c                ó´   — t        j                  «       }|dk(  rt        | «      r| j                  d«      } | j	                  |||¬«       |j                  «       S )zConvert a PIL image to bytes.r   r   ©rB   Úquality)rL   rM   r@   ÚconvertÚsaveÚgetvalue)r?   rB   rT   Útmps       r8   Ú_pil_to_bytesrY   |   sM   € ô �*‰*‹,€Cð �ÒÔ9¸%Ô@Ø—‘˜eÓ$ˆà	‡J�Jˆs˜6¨7€JÔ3à�<‰<‹>Ðr7   c                óD   — | j                  d«       | j                  «       S ©Nr   )ÚseekrW   )Údatas    r8   Ú_bytesio_to_bytesr^   �   s   € Ø‡I�Iˆa„LØ�=‰=‹?Ðr7   c                óœ   — dd l }ddlm}  |j                  | j	                  |j
                  «      «      }t        ||«      }t        ||«      S )Nr   rF   )ÚnumpyrJ   r   Ú	fromarrayÚastypeÚuint8rQ   rY   )ÚarrayÚoutput_formatÚnpr   ÚimgrO   s         r8   Ú_np_array_to_bytesrh   ’   s>   € ÛÝà
ˆ%�/‰/˜%Ÿ,™, r§x¡xÓ0Ó
1€CÜ.¨s°MÓB€Jä˜˜jÓ)Ð)r7   c                óä   — | j                   }t        |«      dvrt        d«      ‚t        |«      dk(  r|d   dvrt        d|d   › d|› �«      ‚t        |«      dk(  r|d   dk(  r| d d …d d …d	f   } | S )
N)é   é   z'Numpy shape has to be of length 2 or 3.rk   r(   )é   rk   é   z#Channel can only be 1, 3, or 4 got z. Shape is rl   r   )ÚshapeÚlenr   )rd   rn   s     r8   Ú_verify_np_shaperp   œ   s…   € ØŸ™€EÜ
ˆ5ƒz˜ÑÜ#Ð$MÓNÐNÜ
ˆ5ƒz�Q‚˜5 ™9¨IÑ5Ü#Ø1°%¸±)°¸KÈÀwÐOó
ð 	
ô
 ˆ5ƒz�Q‚˜5 ™9¨š>Ø’aš˜A�g‘ˆà€Lr7   c                ó(   — d| j                  «       › �S )z2Get the mimetype string for the given ImageFormat.zimage/)Úlower)Úimage_formats    r8   Ú_get_image_format_mimetypert   ¬   s   € à�L×&Ñ&Ó(Ð)Ð*Ð*r7   c                ó¢  — ddl m}  |j                  t        j                  | «      «      }|j
                  \  }}t        |j                  t        «      r|j                  nt        }|dkD  rF||kD  rAt        d|z  |z  |z  «      }|j                  ||f|j                  ¬«      }t        ||d¬«      S |j                  |k7  rt        ||d¬«      S | S )zÛResize an image if it exceeds the given width, or if exceeds
    MAXIMUM_CONTENT_WIDTH. Ensure the image's format corresponds to the given
    ImageFormat. Return the (possibly resized and reformatted) image bytes.
    r   rF   ç      ð?)ÚresampleéZ   rS   )rJ   r   rK   rL   rM   ÚsizerH   ÚwidthÚintr%   ÚresizeÚBILINEARrY   rB   )	rN   Úlayout_configrs   r   rP   Úactual_widthÚactual_heightÚtarget_widthÚ
new_heights	            r8   Ú_ensure_image_size_and_formatrƒ   ±   sÙ   € õ à$˜%Ÿ*™*¤R§Z¡Z°
Ó%;Ó<€IØ"+§.¡.Ñ€L�-ô �m×)Ñ)¬3Ô/ð 	×Òä"ð ð �aÒ˜L¨<Ò7ä˜˜}Ñ,¨|Ñ;¸lÑJÓKˆ
ð
 ×$Ñ$Ø˜:Ð&Ø—^‘^ð %ó 
ˆ	ô ˜Y¨|ÀRÔHÐHà×Ñ˜<Ò'ä˜Y¨|ÀRÔHÐHð Ðr7   c                óš  — dd l }| }t        | j                  j                  |j                  «      rP|r|j                  | dd«      }n3|j                  | «      dk  s|j                  | «      dkD  rt        d«      ‚|dz  }|S |r|j                  | dd«      }|S |j                  | «      dk  s|j                  | «      dkD  rt        d«      ‚|S )Nr   rv   g        z0Data is outside [0.0, 1.0] and clamp is not set.éÿ   z.Data is outside [0, 255] and clamp is not set.)	r`   Ú
issubclassÚdtypeÚtypeÚfloatingÚclipÚaminÚamaxÚRuntimeError)r?   Úclamprf   r]   s       r8   Ú_clip_imager�   Ú   sÂ   € Ûà€DÜ�%—+‘+×"Ñ" B§K¡KÔ0ÙØ—7‘7˜5 ! SÓ)‰DØ�W‰W�U‹^˜cÒ! R§W¡W¨U£^°cÒ%9ÜÐQÓRÐRØ�c‰zˆð
 €Kñ	 
Ø�w‰w�u˜a Ó%ˆð €Kð 
�‰�‹˜!Ò	˜rŸw™w u›~°Ò3ÜÐKÓLÐLØ€Kr7   c                ód  — ddl }ddlm}m} t	        | t
        «      rt        | «      } t	        | t        «      �rt        j                  j                  | «      st        j                  | d¬«      r| S | j                  d«      rCt        j                  j                  | «      r$t        | «      5 }	|	j                  «       } ddd«       t        j                   d| «      rOd| vr| j#                  d	d
d«      } ddl}
|
j'                  | j)                  d«      «      j+                  d«      }d|› �S 	 t        | d«      5 }|j                  «       }ddd«       nÓt	        | |j                  |j                  f«      rt?        | |«      }tA        | |«      }n˜t	        | tB        jD                  «      rtG        | «      }nrt	        | |jH                  «      rZtK        tM        | «      |«      } |dk(  r2tO        | jP                  «      dk(  r| dd…dd…g d¢f   } ntS        d«      ‚tU        | |¬«      }n| }t?        |«      }tW        |||«      }tY        |«      }t3        jZ                  «       rHt3        j4                  «       j6                  j9                  |||«      }t;        j<                  |||«       |S y# 1 sw Y   �ŒêxY w# 1 sw Y   �ŒlxY w# t,        $ rg ddl}|j1                  | «      \  }}|€d}t3        j4                  «       j6                  j9                  | ||«      }t;        j<                  | ||«       |cY S w xY w)a/  Return a URL that an image can be served from.
    If `image` is already a URL, return it unmodified.
    Otherwise, add the image to the MediaFileManager and return the URL.
    (When running in "raw" mode, we won't actually load data into the
    MediaFileManager, and we'll return an empty URL).
    r   N)r   r   )ÚhttpÚhttpsr]   )Úallowed_schemasz.svgz2(^\s?(<\?xml[\s\S]*<svg\s)|^\s?<svg\s|^\s?<svg>\s)Úxmlnsz<svgz(<svg xmlns="http://www.w3.org/2000/svg" rl   zutf-8zdata:image/svg+xml;base64,Úrbzapplication/octet-streamr   rk   )rj   rl   r   zQWhen using `channels="BGR"`, the input image should have exactly 3 color channels)rd   re   Ú ).r`   rJ   r   r   rH   r   ÚstrÚosÚpathÚisfiler   Úis_urlÚendswithrK   ÚreadÚreÚsearchÚreplaceÚbase64Ú	b64encodeÚencodeÚdecodeÚ	ExceptionÚ	mimetypesÚ
guess_typer   Úget_instanceÚmedia_file_mgrÚaddr   Úsave_media_datarQ   rY   rL   rM   r^   Úndarrayr�   rp   ro   rn   r   rh   rƒ   rt   Úexists)r?   r~   rŽ   Úchannelsre   Úimage_idrf   r   r   Útextfiler¡   Úimage_b64_encodedÚfrN   r¦   ÚmimetypeÚ_ÚurlrO   rs   s                       r8   Úimage_to_urlr¶   ë   sÌ  € ó ß$ô
 �%œÔÜ�E“
ˆô �%œÕÜ�w‰w�~‰~˜eÔ$¬¯©ØÐ#<õ*
ð ˆLà�>‰>˜&Ô!¤b§g¡g§n¡n°UÔ&;ä�e“ð ( Ø Ÿ™›�÷(ô
 �9‰9ÐJÈEÔRØ˜eÑ#ð Ÿ™ØÐFÈó�ó à &× 0Ñ 0°·±¸gÓ1FÓ G× NÑ NÈwÓ WÐà/Ð0AÐ/BÐCÐCð	Ü�e˜TÓ"ð & aØŸV™V›X�
÷&øô" 
�E˜I×/Ñ/°·±Ð=Ô	>Ü2°5¸-ÓHˆ
Ü" 5¨*Ó5‰
ô
 
�Eœ2Ÿ:™:Ô	&Ü& uÓ-‰
ô 
�E˜2Ÿ:™:Ô	&ÜÔ,¨UÓ3°UÓ;ˆà�uÒÜ�5—;‘;Ó 1Ò$Øša¢¢I˜oÑ.‘ä+ð4óð ô
 (¨eÀ=ÔQ‰
ð ˆ
ô 1°¸]ÓK€LÜ.¨z¸=È,ÓW€JÜ)¨,Ó7€Hä‡~�~ÔÜ×"Ñ"Ó$×3Ñ3×7Ñ7¸
ÀHÈhÓWˆÜ×Ñ 
¨H°hÔ?Øˆ
à÷[(ñ (ú÷*&ñ &ûäò 	ó à#×.Ñ.¨uÓ5‰KˆH�aØÐØ5�ä×&Ñ&Ó(×7Ñ7×;Ñ;¸EÀ8ÈXÓVˆCÜ×#Ñ# E¨8°XÔ>ØŠJð	ús7   Â,J%Ä+J? Ä7J2ÅJ? Ê%J/Ê2J<Ê7J? Ê?A-L/Ì.L/c           	     ór   — t        | j                  d   «      D �cg c]  }| |d d …d d …d d …f   ‘Œ c}S c c}w r[   )Úrangern   )rd   Úis     r8   Ú_4d_to_list_3drº   \  s0   € Ü',¨U¯[©[¸©^Ó'<Ö= !ˆE�!’Qšš1�*ÓÒ=Ð=ùÒ=s   ›4c           	     ó¤  — ddl }t        d|j                  «       «      }t        |t        t
        t        f«      rt	        |«      }	nGt        ||j                  «      r$t        |j                  «      dk(  rt        |«      }	nt        d|g«      }	t        |t        «      r|}
nqt        |t        «      r|g}
n]t        ||j                  «      r)t        |j                  «      dk(  r|j                  «       }
n|€dgt        |	«      z  }
nt        |«      g}
t        |
t        «      st        d«      ‚t        |
«      t        |	«      k7  r$t        dt        |
«      › d	t        |	«      › d
�«      ‚t        t        |	|
d¬«      «      D ]P  \  }\  }}|j                   j#                  «       }|�t        |«      |_        | › d|› �}t'        ||||||«      |_        ŒR y)aØ  Fill an ImageListProto with a list of images and their captions.
    The images will be resized and reformatted as necessary.

    Parameters
    ----------
    coordinates
        A string identifying the images' location in the frontend.
    image
        The image or images to include in the ImageListProto.
    caption
        Image caption. If displaying multiple images, caption should be a
        list of captions (one for each image).
    width
        The desired width of the image or images. This parameter will be
        passed to the frontend.
        Positive values set the image width explicitly.
        Negative values has some special. For details, see: `WidthBehaviour`
    proto_imgs
        The ImageListProto to fill in.
    clamp
        Clamp image pixel values to a valid range ([0-255] per channel).
        This is only meaningful for byte array images; the parameter is
        ignored for image URLs. If this is not set, and an image has an
        out-of-range value, an error will be thrown.
    channels
        If image is an nd.array, this parameter denotes the format used to
        represent color information. Defaults to 'RGB', meaning
        `image[:, :, 0]` is the red channel, `image[:, :, 1]` is green, and
        `image[:, :, 2]` is blue. For images coming from libraries like
        OpenCV you should set this to 'BGR', instead.
    output_format
        This parameter specifies the format to use when transferring the
        image data. Photos should use the JPEG format for lossy compression
        while diagrams should use the PNG format for lossless compression.
        Defaults to 'auto' which identifies the compression type based
        on the type and format of the image argument.
    r   Nr   rm   zSequence[AtomicImage]rl   z9If image is a list then caption should be a list as well.zCannot pair z captions with z images.F)Ústrictú-)r`   r   rG   rH   ÚlistÚsetÚtupler¬   ro   rn   rº   r—   Útolistr   Ú	enumerateÚzipÚimgsrª   Úcaptionr¶   rµ   )Úcoordinatesr?   rÅ   r~   Ú
proto_imgsrŽ   r®   re   rf   ÚimagesÚcaptionsÚcoord_suffixÚsingle_imageÚsingle_captionÚ	proto_imgr¯   s                   r8   Úmarshall_imagesrÎ   `  sª  € ó^ ä�J §¡Ó 0Ó1€Hô �%œ$¤¤UÐ+Ô,Ü�e“‰Ü	�E˜2Ÿ:™:Ô	&¬3¨u¯{©{Ó+;¸qÒ+@Ü Ó&‰äÐ-°¨wÓ7ˆä�'œ4Ô Ø)0‰Ü	�GœSÔ	!Ø�9‰Ü	�G˜RŸZ™ZÔ	(¬S°·±Ó-?À1Ò-DØ—>‘>Ó#‰Ø	ˆØ�6œC ›KÑ'‰ä˜“L�>ˆä�h¤Ô%Ü#ØGó
ð 	
ô ˆ8ƒ}œ˜F›Ò#Ü#Øœ3˜x›=˜/¨¼¸V»¸ÀXÐNó
ð 	
ô
 9BÜˆF�H UÔ+ó9ò 
Ñ4ˆÑ4�| ^ð —O‘O×'Ñ'Ó)ˆ	ØÐ%Ü # NÓ 3ˆIÔð "�] ! L >Ð2ˆä$Ø˜-¨°¸-Èó
ˆ	�ñ
r7   )r?   r   ÚreturnÚbool)rN   zbytes | PILImagerB   r—   rÏ   r!   )r   éd   )r?   r   rB   r!   rT   r{   rÏ   rI   )r]   z
io.BytesIOrÏ   rI   )r   )rd   r   re   r—   rÏ   rI   )rd   r   rÏ   r   )rs   r!   rÏ   r—   )rN   rI   r~   r   rs   r!   rÏ   rI   )r?   r   rŽ   rÐ   rÏ   r   )r?   r   r~   r   rŽ   rÐ   r®   r   re   r#   r¯   r—   rÏ   r—   )rd   r   rÏ   zlist[npt.NDArray[Any]])r   r"   )rÆ   r—   r?   r$   rÅ   z)str | npt.NDArray[Any] | list[str] | Noner~   r   rÇ   ÚImageListProtorŽ   rÐ   r®   r   re   r#   rÏ   ÚNone)DÚ
__future__r   rL   r˜   rž   Úcollections.abcr   Úenumr   Úpathlibr   Útypingr   r   r	   r
   r   r   Ú	streamlitr   r   Ústreamlit.errorsr   Ústreamlit.runtimer   r   Únumpy.typingÚnptrJ   r   r   r   Ú#streamlit.elements.lib.layout_utilsr   Ústreamlit.proto.Image_pb2r   rÒ   Ústreamlit.type_utilr   r   Ú__annotations__rM   r—   rI   r   r   r!   r#   r$   r%   r'   r1   r0   r2   r3   r@   rC   rQ   rY   r^   rh   rp   rt   rƒ   r�   r¶   rº   rÎ   r6   r7   r8   ú<module>râ      so  ðö #ã 	Û 	Û 	Ý $Ý Ý ß H× Hç 'Ý 2Ý %áÝåß4Ñ4å@ÝEÝ.àØGñ€ˆ)ó ð ØÐ  "§*¡*¨c°4¸Ð>ñ€ˆYó ð ˜lÑ+€ˆ)Ó +Ø Ð!5Ñ6€ˆYÓ 6Ø& {°FÐ':Ñ;Ð �9Ó ;Ø)¨H°[Ñ,AÑAÐ �)Ó Að %,Ð �zÓ +ô

 �Gô 
 ð "O€× Ñ Ô à?ð × Ñ Ô ð€× Ñ Ô ó
-ó!ðØ ðØ*-ðàóðH !ØðØðàðð ðð ó	ó"ô
*óó +ð
&Øð&Ø&2ð&ØBMð&à
ó&óRð"nØðnàðnð ðnð ð	nð
 %ðnð ðnð 	ónób>ð Ø'-ð_
Øð_
àð_
ð 7ð_
ð  ð	_
ð
 ð_
ð ð_
ð ð_
ð %ð_
ð 
ô_
r7   