o
    ۷i.                     @  s^  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlZd dlZd dlZd dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ ej  dMddZ 		dNdOddZ!	dPdQd!d"Z"dRd&d'Z#dSd,d-Z$dTd/d0Z%dUd2d3Z&dPdVd4d5Z'dWd7d8Z(dXd9d:Z)dYd=d>Z*dYd?d@Z+dZdEdFZ,	d[d\dKdLZ-dS )]    )annotationsN)BytesIO)Path)Literalcast)quote)get_mimetypeis_http_url_like)ImageOps)processing_utils)WatermarkOptions)	ImageData)Errororig_img)np.ndarray | PIL.Image.Image | str | PathreturnPIL.Image.Imagec                 C  sd   t | tjrtj| }|S t | ttfrtj| }|S t | tjjr(| }|S t	dtt
|  )z
    Provided an array, PIL Image or filepath, return a PIL Image.
    Parameters:
        orig_img: Local image file. If a filepath, it must be a webp, png, jpeg, or bmp.
    Returns:
        open_img: A PIL.Image.Image.
    zeExpected image or path to image of type webp, png, bmp or jpeg; PIL image; or numpy array. Received  )
isinstancenpndarrayPILImage	fromarraystrr   open
ValueErrortype)r   open_img r   H/home/ubuntu/vllm_env/lib/python3.10/site-packages/gradio/image_utils.py
open_image   s   	

r    imagewebpimPIL.Image.Image | Noner   #Literal['numpy', 'pil', 'filepath']	cache_dirr   nameformat)np.ndarray | PIL.Image.Image | str | Nonec              	   C  s   | du r| S |dkr| S |dkrt | S |dkr;ztj| |||d}W |S  ttfy:   tj| ||dd}Y |S w tdt| d )	z3Helper method to format an image based on self.typeNpilnumpyfilepath)r&   r'   r(   pngzUnknown type: z1. Please choose from: 'numpy', 'pil', 'filepath'.)r   arrayr   save_pil_to_cacheKeyErrorr   r   )r#   r   r&   r'   r(   pathr   r   r   format_image/   s8   
	r2   yc              	   C  s   t | tjrtj| ||d}|S t | tjjr7ztj| ||d}W |S  tt	fy6   tj| |dd}Y |S w t | t
rBt| }|S t | trK| }|S t	dtt|  )Nr&   r(   r-   z6Cannot process this value as an Image, it is of type: )r   r   r   r   save_img_array_to_cacher   r   r/   r0   r   r   r   r   )r3   r&   r(   r1   r   r   r   
save_imageS   s4   

r6   base_imgwatermark_optionr   c                 C  s\  t | } | j\}}t ttjtjjB tB tB |j	|_	|j	j\}}t
|jtred}|jdkr4||}}n6|jdkrC|| | |}}n'|jdkrR||| | }}n|jdkrd|| | || | }}n|j\}}|dk s~|| |ks~|dk s~|| |kr|| d }|| d }||f}	| j}
| d} |j	d|_	| j|j	|	|j	d | |
} | S )	aw  Overlays a watermark image on a base image.
    Parameters:
        base_img: Base image onto which the watermark is applied. Can be an array, PIL Image, or filepath.
        watermarkOption: WatermarkOptions instance containing watermark image and position settings.
    Returns:
        watermarked_img: A PIL Image of the base image overlaid with the watermark image.
    
   ztop-leftz	top-rightzbottom-leftzbottom-rightr   RGBA)mask)r    sizer   r   r   r   r   r   r   	watermarkr   positionmodeconvertpaste)r7   r8   base_img_widthbase_img_heightwatermark_widthwatermark_heightpaddingxr3   watermark_positionorig_img_moder   r   r   add_watermarkp   sF   










rJ   imgfinal_widthintfinal_heightc                 C  s   | j \}}|| }|| |kr|}|| }n|}|| }|| d }|| d }	| t|t|	t|| t|	| f}
|
||f}|S )N   )r<   croprM   resize)rK   rL   rN   original_widthoriginal_heighttarget_aspect_ratiocrop_height
crop_widthlefttopimg_croppedimg_resizedr   r   r   
crop_scale   s   

"r[   encodingc                 C  sf   t | }tjtt|}ztt	drt	
|}W n ty*   td| Y nw |d us1J |S )Nexif_transposez0Failed to transpose image %s based on EXIF data.)r   extract_base64_datar   r   r   r   base64	b64decodehasattrr
   r]   	Exceptionprint)r\   image_encodedrK   r   r   r   decode_base64_to_image   s   


re   
np.ndarrayc                 C  s   t | }t|S N)re   r   asarray)r\   rK   r   r   r   decode_base64_to_image_array   s   
ri   c                 C  s   t | }t|||S rg   )re   r6   )r\   r&   r(   rK   r   r   r   decode_base64_to_file   s   rj   image_arrayc                 C  sl   t  }tjtj| tjdd}||d |	 }W d    n1 s%w   Y  t
t|d}d| S )NF)
force_copyJPEGutf-8data:image/jpeg;base64,)r   r   r   r   r   _convertr   uint8savegetvaluer   r_   	b64encode)rk   output_bytes	pil_image
bytes_data
base64_strr   r   r   encode_image_array_to_base64   s   
ry   c                 C  sR   t  }| |d | }W d    n1 sw   Y  tt|d}d| S )Nrm   rn   ro   )r   rr   rs   r   r_   rt   )r!   ru   rw   rx   r   r   r   encode_image_to_base64   s   
rz   
image_file
str | Pathc                 C  s^   t t| }t| d}| }W d    n1 sw   Y  tt|d}d| d| S )Nrbrn   data:z;base64,)r   r   r   readr_   rt   )r{   	mime_typefrw   rx   r   r   r   encode_image_file_to_base64   s   
r   c                 C  s^   t | } t| rt| }|  |jS t| }| }W d   |S 1 s(w   Y  |S )z
    Provided a path or URL to an SVG file, return the SVG content as a string.
    Parameters:
        image_file: Local file path or URL to an SVG file
    Returns:
        str: The SVG content as a string
    N)r   r	   httpxgetraise_for_statustextr   r   )r{   responsefilesvg_contentr   r   r   extract_svg_content   s   



r   payloadImageData | None
image_modeULiteral['1', 'L', 'P', 'RGB', 'RGBA', 'CMYK', 'YCbCr', 'LAB', 'HSV', 'I', 'F'] | Nonec                 C  s  | d u r| S | j r,| j dr,|dkrt| j S |dkr!t| j S |dkr,t| j ||S | jd u r5tdt| j}| jrSt| j}|j	}|j
dd}|dv rRd	}nd
}d}| dkri|dkret|S tdtj|}	|dkr~|d |	jfv r~t|S |	 }
|
dddkrttdrzt|	}	W n ty   td| d Y nw | dkr|	d urt  td |d ur|	|}	W d    n1 sw   Y  t|	||||dS )Nr~   r*   r+   r,   zImage path is None.. )jpgjpegr   r!   r"   svgz9SVG files are not supported as input images for this app.i     r]   zFailed to transpose image z based on EXIF data.gifignore)r   r&   r'   r(   )url
startswithre   ri   rj   r1   r   r   	orig_namestemsuffixreplacelowerr   r   r   r   r   r?   getexifr   ra   r
   r]   rb   warningswarncatch_warningssimplefilterr@   r2   )r   r&   r(   r   r   	file_pathpr'   r   r#   exifr   r   r   preprocess_image  sb   








r   value0np.ndarray | PIL.Image.Image | str | Path | Noner=   WatermarkOptions | Nonec                 C  s   ddl m} | du rdS t| tr2|  dr2t| }|dur$|d tt| j	dt
| dS |r>|jdur>t| |} t| ||d}t| rPt|j	nd}t||d	S )
a+  
    Parameters:
        value: Expects a `numpy.array`, `PIL.Image`, or `str` or `pathlib.Path` filepath to an image which is displayed.
        watermark: An optional `WatermarkOptions` instance to apply a watermark to the image.
    Returns:
        Returns the image as a `FileData` object.
    r   )WarningNz.svgzUWatermarking for SVG images is currently not supported. No watermark will be applied.zdata:image/svg+xml,)r   r   r4   )r1   r   )gradior   r   r   r   endswithr   r   r   r'   r   r=   rJ   r6   exists)r   r&   r(   r=   r   r   savedr   r   r   r   postprocess_imageC  s$   
r   )r   r   r   r   )r!   r"   )r#   r$   r   r%   r&   r   r'   r   r(   r   r   r)   )r"   )r3   r   r&   r   r(   r   )r7   r   r8   r   r   r   )rK   r   rL   rM   rN   rM   )r\   r   r   r   )r\   r   r   rf   )r\   r   r&   r   r(   r   r   r   )rk   rf   r   r   )r!   r   r   r   )r{   r|   r   r   )r   r   r&   r   r(   r   r   r   r   r%   r   r)   rg   )
r   r   r&   r   r(   r   r=   r   r   r   ).
__future__r   r_   r   ior   pathlibr   typingr   r   urllib.parser   r   r+   r   	PIL.Imager   gradio_client.utilsr   r	   r
   r   r   gradio.components.image_editorr   gradio.data_classesr   gradio.exceptionsr   r   initr    r2   r6   rJ   r[   re   ri   rj   ry   rz   r   r   r   r   r   r   r   r   <module>   sF    

%

7






B