
    :i                     (   S SK r S SKrS SKrS SKJr  S SKJrJrJr  S SK	J
r
  S SKJr  S\S\R                  4S jrS	\R                  \-  \-  S\R                  4S
 jrS\R                  S\S\4S jr\
\R                  \" \5      \" \5      S4   rg)    N)Image)BeforeValidatorPlainSerializerSerializationInfo)	Annotated)__version__urlreturnc                 t   SS[          30n [        R                  " XS9nUR                  5         [        R
                  " [        R                  " UR                  5      5      nU$ ! [        R                  R                   a  n[        SU  SU S35      eSnAf[         a  n[        SU 35      eSnAff = f)	zDownload an image from a URL and return it as a PIL Image.

Args:
    url: The URL of the image to download.

Returns:
   The downloaded image as a PIL Image object.
z
User-Agentzmistral-common/)headersz!Error downloading the image from z: .NzError converting to PIL image: )r   requestsgetraise_for_statusr   openioBytesIOcontent
exceptionsRequestExceptionRuntimeError	Exception)r	   r   responseimges        N/home/james-whalen/.local/lib/python3.13/site-packages/mistral_common/image.pydownload_imager      s     {m<=GB<<5!!# jjH$4$456
// L>se2aSJKK B<QC@AABs$   AA' 'B7BB7$B22B7xc           	         [        U [        R                  5      (       a  U $ [        U [        5      (       a+   [        R                  " [        R
                  " U 5      5      $  [        R                  " [        R
                  " [        R                  " U R                  S5      5      5      5      nU$ ! [         a    [        S5      ef = f! [         a  n[        SU SS  S35      UeSnAff = f)a9  Load an image from a string or bytes.

If the input is already a PIL Image, return it as is.

Args:
    x: The input to load the image from. Can be a PIL Image, a string, or bytes.
        If it's a string, it's assumed to be a base64 encoded string of bytes.

Returns:
   The loaded image as a PIL Image object.
z3Encountered an error when loading image from bytes.asciizBEncountered an error when loading image from bytes starting with 'N   zI'. Expected either a PIL.Image.Image or a base64 encoded string of bytes.)
isinstancer   bytesr   r   r   r   r   base64	b64decodeencode)r   imager   s      r   "maybe_load_image_from_str_or_bytesr(   %   s     !U[[!!!U	V::bjjm,,

2::f&6&6qxx7H&IJK  	VTUU	V  sVH '(
 		s$   )B2 #AC 2C
C-C((C-iminfoc                    [        US5      (       a  UR                  =(       d    0 nO0 n[        R                  " 5       nU R                  =(       d    SnU R                  X4S9  [        R                  " UR                  5       5      R                  S5      nU(       a   UR                  S5      =n(       a  USU S-   $ U(       a-  UR                  S5      (       a  S	UR                  5        S
3U-   nU$ )zSerialize an image to a base64 encoded string of bytes.

Args:
    im: The image to serialize.
    info: The serialization info.

Returns:
    The serialized image as a base64 encoded string of bytes.
contextPNG)formatr    max_image_b64_lenNz...add_format_prefixzdata:image/z;base64,)hasattrr,   r   r   r.   saver$   	b64encodegetvaluedecoder   lower)r)   r*   r,   stream	im_formatim_b64r/   s          r   serialize_image_to_byte_strr:   D   s     tY,,$"ZZ\F		"UIGGFG%foo/077@F5H)II%I(()E117;;233y01:VCM    zCA normal PIL image that supports serialization to b64 bytes string.)r$   r   r   PILr   pydanticr   r   r   typing_extensionsr   mistral_commonr   strr   r#   r(   r:   SerializableImage r;   r   <module>rC      s     	   H H ' &B B B2%++*;e*C  >EKK 7H S 4 	KK67/0IK r;   