
    Fg                    J   S SK Jr  S SKrS SKJr  S SKJr  S SKJrJ	r	  S SK
r
S SKrS SKrS SKJrJr  S SKJr  S SKJr  \R*                  R-                  5           S           SS	 jjr S     SS
 jjrSS jrSS jrSS jrSSS jjrSS jrSS jrSS jrSS jr g)    )annotationsN)BytesIO)Path)Literalcast)get_mimetypeis_http_url_like)ImageOps)processing_utilsc                    U c  U $ US:X  a  U $ US:X  a  [         R                  " U 5      $ US:X  a   [        R                  " XX4S9nU$ [        S[        U5      -   S-   5      e! [        [
        4 a    [        R                  " U UUSS9n U$ f = f)z3Helper method to format an image based on self.typepilnumpyfilepath)	cache_dirnameformatpngzUnknown type: z1. Please choose from: 'numpy', 'pil', 'filepath'.)nparrayr   save_pil_to_cacheKeyError
ValueErrorstr)imtyper   r   r   paths         c/Users/admin/workspace/ai/PDFMathTranslate/myenv/lib/python3.13/site-packages/gradio/image_utils.pyformat_imager      s     
z	u}		xx|			#55dD $iAB
 	
 *% 	#55#	D 	s   A" "'BBc                   [        U [        R                  5      (       a  [        R                  " XUS9nU$ [        U [
        R                  R                  5      (       a   [        R                  " XUS9nU$ [        U [        5      (       a  [        U 5      nU$ [        U [        5      (       a  U nU$ [        S[        [        U 5      5      -   5      e! [        [        4 a    [        R                  " XSS9n U$ f = f)N)r   r   r   z6Cannot process this value as an Image, it is of type: )
isinstancer   ndarrayr   save_img_array_to_cachePILImager   r   r   r   r   r   )yr   r   r   s       r   
save_imager&   7   s     !RZZ  776
, K' 
Asyy	'	'	#55vD" K 
At		1v K 
As		 K	 Ds4PQ7|S
 	
 *% 	#55uD K	s   !C %C<;C<c           	        U R                   u  p4X-  nX4-  U:  a  UnXe-  nOUnXu-  nX7-
  S-  nXF-
  S-  n	U R                  [        U5      [        U	5      [        X-   5      [        X-   5      45      n
U
R                  X45      nU$ )N   )sizecropintresize)imgfinal_widthfinal_heightoriginal_widthoriginal_heighttarget_aspect_ratiocrop_height
crop_widthlefttopimg_croppedimg_resizeds               r   
crop_scaler9   T   s    &)hh#N%4'*==% 6
#
 6'1,D(A
-C((	TCHc$"34c#:K6LMK $$k%@AK    c                ~   [         R                  " U 5      n[        R                  R	                  [        [        R                  " U5      5      5      n [        [        S5      (       a  [        R                  " U5      n[        [        R                  R                  U5      $ ! [         a    [        SU5         N<f = f)Nexif_transposez0Failed to transpose image %s based on EXIF data.)r   extract_base64_datar#   r$   openr   base64	b64decodehasattrr
   r<   	Exceptionprintr   )encodingimage_encodedr-   s      r   decode_base64_to_imagerF   k   s    $88BM
))..!1!1-!@A
BC
8-..))#.C 		%%  
>	

s   +B# #B<;B<c                D    [        U 5      n[        R                  " U5      $ N)rF   r   asarray)rD   r-   s     r   decode_base64_to_image_arrayrJ   y   s    
 
*C::c?r:   c                0    [        U 5      n[        X1U5      $ rH   )rF   r&   )rD   r   r   r-   s       r   decode_base64_to_filerL   ~   s    
 
*Ccf--r:   c           	     Z   [        5        n[        R                  R                  [        R
                  " U [        R                  SS95      nUR                  US5        UR                  5       nS S S 5        [        [        R                  " W5      S5      nSU-   $ ! , (       d  f       N3= f)NF)
force_copyJPEGutf-8data:image/jpeg;base64,)r   r#   r$   	fromarrayr   _convertr   uint8savegetvaluer   r?   	b64encode)image_arrayoutput_bytes	pil_image
bytes_data
base64_strs        r   encode_image_array_to_base64r]      s    	lII''%%k288N
	 	|V,!**,
 
 V%%j17;J$z11 
s   A$B
B*c                    [        5        nU R                  US5        UR                  5       nS S S 5        [        [        R
                  " W5      S5      nSU-   $ ! , (       d  f       N3= f)NrO   rP   rQ   )r   rU   rV   r   r?   rW   )imagerY   r[   r\   s       r   encode_image_to_base64r`      sV    	l

<(!**,
 
 V%%j17;J$z11	 
s   #A
A)c                    [        [        U 5      5      n[        U S5       nUR                  5       nS S S 5        [        [        R
                  " W5      S5      nSU S3U-   $ ! , (       d  f       N7= f)NrbrP   zdata:z;base64,)r   r   r>   readr?   rW   )
image_file	mime_typefr[   r\   s        r   encode_image_file_to_base64rg      s`    S_-I	j$	1VVX
 
 V%%j17;J9+X&33 
 	s   A##
A1c                   [        U 5      n [        U 5      (       a2  [        R                  " U 5      nUR	                  5         UR
                  $ [        U 5       nUR                  5       nSSS5        U$ ! , (       d  f       W$ = f)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>   rc   )rd   responsefilesvg_contents       r   extract_svg_contentrp      sk     ZJ
##99Z(!!#}}*))+K  s   A44
B)r_   webp)r   zPIL.Image.Image | Noner   z#Literal['numpy', 'pil', 'filepath']r   r   r   r   r   r   returnz)np.ndarray | PIL.Image.Image | str | None)rq   )r%   z)np.ndarray | PIL.Image.Image | str | Pathr   r   r   r   )r-   PIL.Image.Imager.   r+   r/   r+   )rD   r   rr   rs   )rD   r   rr   
np.ndarray)rD   r   r   r   r   r   rr   r   )rX   rt   rr   r   )r_   rs   rr   r   )rd   z
str | Pathrr   r   )!
__future__r   r?   ior   pathlibr   typingr   r   ri   r   r   	PIL.Imager#   gradio_client.utilsr   r	   r
   gradior   r$   initr   r&   r9   rF   rJ   rL   r]   r`   rg   rp    r:   r   <module>r~      s    "         >  # 		  !
!

-!
 !
 	!

 !
 /!
J QW0=@JM:.&
.
224r:   