
    gN=                       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 d dlmZ d dlmZmZ d d	l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% ed   Z&de'd<   ee&dejP                  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e+   f   Z/de'd<   dZ0de'd<    G d de      Z1d e1jd                  _3        d!e1jh                  _3        d"e1jj                  _3        d0d#Z6d0d$Z7	 	 	 	 	 	 d1d%Z8	 	 d2	 	 	 	 	 	 	 d3d&Z9d4d'Z:d5d6d(Z;d7d)Z<d8d*Z=	 	 	 	 	 	 	 	 d9d+Z>d:d,Z?	 	 	 	 	 	 	 	 	 	 	 	 	 	 d;d-Z@d<d.ZA	 	 d=	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d>d/ZBy)?    )annotationsN)Sequence)IntEnum)Path)TYPE_CHECKINGFinalLiteralUnioncast)	TypeAlias)runtimeurl_util)StreamlitAPIException)caching)
NumpyShape)Any)GifImagePluginImage	ImageFile)	ImageList)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     W/var/www/openai/venv/lib/python3.12/site-packages/streamlit/elements/lib/image_utils.pyr&   r&   =   s$    
 HFDr6   r&   z'Display the image at its original widthz5Display the image at the width of the column it's in.zDisplay 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    r7   _image_may_have_alpha_channelr?   S   s    ::,,,r6   c                     | j                   dk(  S )Nr   )formatr=   s    r7   _image_is_gifrB   W   s    <<5  r6   c                   |j                         }|dv rt        t        |      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   JPGr   r   r   r   r   )upperr   r    
isinstancebytesPILr   openioBytesIOrB   r?   )
image_datarA   r   	pil_images       r7   _validate_image_format_stringrO   [   sv     \\^F K((  *e$EJJrzz*56		Y$Y/r6   c                    t        j                         }|dk(  rt        |       r| j                  d      } | j	                  |||       |j                         S )zConvert a PIL image to bytes.r   r   rA   quality)rK   rL   r?   convertsavegetvalue)r>   rA   rR   tmps       r7   _PIL_to_bytesrW   }   sM     **,C 9%@e$	JJs67J3<<>r6   c                D    | j                  d       | j                         S Nr   )seekrU   )datas    r7   _BytesIO_to_bytesr\      s    IIaL==?r6   c                    dd l }ddlm}  |j                  | j	                  |j
                              }t        ||      }t        ||      S )Nr   rE   )numpyrI   r   	fromarrayastypeuint8rO   rW   )arrayoutput_formatnpr   imgrA   s         r7   _np_array_to_bytesrf      s>    
%//%,,rxx0
1C*3>Ff%%r6   c                    | j                   }t        |      dvrt        d      t        |      dk(  r#|d   dvrt        d|d   t        |      fz        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.ri   r'   )   ri      z2Channel can only be 1, 3, or 4 got %d. Shape is %srj   r   )shapelenr   str)rb   rl   s     r7   _verify_np_shapero      s    E
5z#$MNN
5zQ59I5#@Ry#e*%&
 	
 5zQ59>aAgLr6   c                (    d| j                          S )z2Get the mimetype string for the given ImageFormat.zimage/)lower)image_formats    r7   _get_image_format_mimetypers      s    L&&()**r6   c                r   ddl m}  |j                  t        j                  |             }|j
                  \  }}|dk  r|t        kD  rt        }|dkD  rF||kD  rAt        d|z  |z  |z        }|j                  ||f|j                        }t        ||d      S |j                  |k7  rt        ||d      S | S )zResize 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   rE         ?)resampleZ   rQ   )rI   r   rJ   rK   rL   sizer$   intresizeBILINEARrW   rA   )rM   widthrr   r   rN   actual_widthactual_height
new_heights           r7   _ensure_image_size_and_formatr      s     $%**RZZ
%;<I"+..L-qy\$99%qy\E)},u4|CD

 $$eZ%85>>$R	Y|RHH<'Y|RHH r6   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   ru   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>   clamprd   r[   s       r7   _clip_imager      s    D%++""BKK0775!S)Dwwu~#rwwu~';"#UVVcz K 775!S)D K wwu~!RWWU^c%9"#STTKr6   c                   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       nt	        | |j                  |j                  f      rt?        | |      }tA        | |      }nt	        | tB        jD                        rtG        |       }nt	        | |jH                        rhtK        tM        |       |      } |dk(  r@tO        tQ        tR        | jT                              dk(  r| ddddg df   } ntW        d      tY        | |      }n| }t?        |      }t[        |||      }t]        |      }t3        j^                         rHt3        j4                         j6                  j9                  |||      }t;        j<                  |||       |S y# 1 sw Y   xY w# 1 sw Y   zx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" rj   zutf-8zdata:image/svg+xml;base64,rbzapplication/octet-streamr   ri   )rh   rj   r   zQWhen using `channels="BGR"`, the input image should have exactly 3 color channels)rb   rc    )0r^   rI   r   r   rG   r   rn   ospathisfiler   is_urlendswithrJ   readresearchreplacebase64	b64encodeencodedecode	Exception	mimetypes
guess_typer   get_instancemedia_file_mgraddr   save_media_datarO   rW   rK   rL   r\   ndarrayr   ro   rm   r   r   rl   r   rf   r   rs   exists)r>   r|   r   channelsrc   image_idrd   r   r   textfiler   image_b64_encodedfrM   r   mimetype_urlrA   rr   s                       r7   image_to_urlr      s    $
 %E
 %ww~~e$#<*
 L>>&!bggnnU&;e  
 99JERe# F  & 0 0g1F G N Nw W/0A/BCC	eT"aVVX
 #" 
EI//=	>.umD"5&1

 
E2::	&&u-
 
E2::	&,U3U;u4
EKK01Q6aIo.+4 
 (e=Q
 
 1]KL.z5,OJ),7H~~""$3377
HhW
Hh?
 ] * #" 	 #..u5KHa5&&(77;;E8XVC##E8X>J	s7   ,J3+K 7K K 3J= K
K A-L=<L=c           	     t    t        d| j                  d         D cg c]  }| |d d d d d d f    c}S c c}w rY   )rangerl   )rb   is     r7   _4d_to_list_3dr   Z  s8    ',QA'?@'?!E!Q1*'?@@@s   5c           	        ddl }t        t        |j                               }t	        |t
        t        t        f      rt        |      }	nKt	        ||j                        r2t        t        t        |j                              dk(  rt        |      }	n|g}	t	        |t
              r|}
nt	        |t              r|g}
nkt	        ||j                        r7t        t        t        |j                              dk(  r|j                         }
n|dgt        |	      z  }
nt        |      g}
t	        |
t
              sJ d       t        |
      t        |	      k(  sJ dt        |
      t        |	      fz         t        |      |_        t#        t%        |	|
            D ]P  \  }\  }}|j&                  j)                         }|t        |      |_        d| |fz  }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 indentifying 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   Nrk   rj   z1If image is a list then caption should be as wellz'Cannot pair %d captions with %d images.z%s-%i)r^   r   r   rF   rG   listsettupler   rm   r   rl   r   rn   tolistry   r|   	enumeratezipimgsr   captionr   r   )coordinatesr>   r   r|   
proto_imgsr   r   rc   rd   imagescaptionscoord_suffix	proto_imgr   s                 r7   marshall_imagesr   ^  s   \ Hhnn./H %$U+,e	E2::	&3tJ/L+MQR+R&'4 )0	GS	!9	GRZZ	(Sj'--1P-QUV-V>>#	6CK'L>h% ;% x=CK' )RHFV * '
 5zJ*3C4I*J&&ugOO'')	 #GI k<88$5%=(
	 +Kr6   )r>   r   returnbool)rM   zbytes | PILImagerA   rn   r   r    )r   d   )r>   r   rA   r    rR   ry   r   rH   )r[   z
io.BytesIOr   rH   )r   )rb   r   rc   rn   r   rH   )rb   r   r   r   )rr   r    r   rn   )rM   rH   r|   ry   rr   r    r   rH   )r>   r   r   r   r   r   )r>   r   r|   ry   r   r   r   r   rc   r"   r   rn   r   rn   )rb   r   r   zlist[npt.NDArray[Any]])r   r!   )r   rn   r>   r#   r   z)str | npt.NDArray[Any] | list[str] | Noner|   zint | WidthBehaviorr   ImageListProtor   r   r   r   rc   r"   r   None)C
__future__r   rK   r   r   collections.abcr   enumr   pathlibr   typingr   r   r	   r
   r   typing_extensionsr   	streamlitr   r   streamlit.errorsr   streamlit.runtimer   streamlit.type_utilr   r   numpy.typingnptrI   r   r   r   streamlit.proto.Image_pb2r   r   r   __annotations__rL   rn   rH   r   r   r    r"   r#   r$   r&   r0   r/   r1   r2   r?   rB   rO   rW   r\   rf   ro   rs   r   r   r   r   r   r5   r6   r7   <module>r      su   # 	 	 	 $   = = ' ' 2 % *44EG)   "**c4>Y  l+) + !56Y 6&{F':; 9 ;#K+1F$FG ) G %, z +

 G 
  "O   ?       
-! *-H !  	"
&"+
!1<
B(ooo o 	o
 %o o 	odA '-[
[
[
 7[
 	[

 [
 [
 [
 %[
 
[
r6   