o
    #1 i                     @   s   d dl Z d dlZd dlmZ d dl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edejfd	d
Zdeejeef dejfddZdejde
defddZeejeee	edf ZdS )    N)Union)Image)BeforeValidatorPlainSerializerSerializationInfo)	Annotated)__version__urlreturnc              
   C   s   ddt  i}ztj| |d}|  tt|j}|W S  tj	j
y7 } ztd|  d| dd}~w tyI } ztd| d}~ww )	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e r   `/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/mistral_common/image.pydownload_image   s   	r   xc              
   C   s   t | tjr| S t | tr"z	tt| W S  ty!   tdw zttt	| 
d}|W S  tyL } ztd| dd  d|d}~ww )aU  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   r   r   r   "maybe_load_image_from_str_or_bytes&   s*   

r'   iminfoc                 C   s   t |dr|jp	i }ni }t }| jpd}| j||d t| 	d}|r8|
d }r8|d| d S |rI|
drId	|  d
| }|S )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.
    contextZPNG)formatr   max_image_b64_lenNz...Zadd_format_prefixzdata:image/z;base64,)hasattrr*   r   r   r+   saver#   	b64encodegetvaluedecoder   lower)r(   r)   r*   streamZ	im_formatZim_b64r,   r   r   r   serialize_image_to_byte_strE   s   


r4   zCA normal PIL image that supports serialization to b64 bytes string.)r#   r   typingr   r   ZPILr   Zpydanticr   r   r   Ztyping_extensionsr   Zmistral_commonr   strr   r"   r'   r4   ZSerializableImager   r   r   r   <module>   s$     