
    wil!                    H   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	 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 ej                                         	 	 d6d7dZ	 d8d9dZd:d!Zd;d#Z d<d%Z!d8d=d&Z"d>d(Z#d?d)Z$d@d,Z%d@d-Z&dAd2Z'dBd5Z(dS )C    )annotationsN)BytesIO)Path)Literal)quote)get_mimetypeis_http_url_like)ImageOps)processing_utils)	ImageData)ErrorimagewebpimPIL.Image.Image | Nonetype#Literal['numpy', 'pil', 'filepath']	cache_dirstrnameformatreturn)np.ndarray | PIL.Image.Image | str | Nonec                4   | | S |dk    r| S |dk    rt          j        |           S |dk    rK	 t          j        | |||          }n/# t          t
          f$ r t          j        | ||d          }Y nw xY w|S t          dt          |          z   dz             )	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'.)nparrayr   save_pil_to_cacheKeyError
ValueErrorr   )r   r   r   r   r   paths         b/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/gradio/image_utils.pyformat_imager&      s     
z	u}}		x||				#5id6  DD *% 	 	 	#5#	  DDD	 $iiAB
 
 	
s   A )A32A3y)np.ndarray | PIL.Image.Image | str | Pathc                   t          | t          j                  rt          j        | ||          }nt          | t
          j        j                  rG	 t          j        | ||          }n# t          t          f$ r t          j        | |d          }Y nmw xY wt          | t                    rt          |           }nDt          | t                    r| }n,t          dt          t          |                     z             |S )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   )r'   r   r   r$   s       r%   
save_imager0   ;   s!    !RZ   
76
 
 
 
Asy	'	' 
	#5Yv  DD *% 	 	 	#5Yu  DDD	 
At		 
1vv	As		 
Ds4PQ77||S
 
 	
 Ks   A+ +(BBimgPIL.Image.Imagefinal_widthintfinal_heightc           	     H   | j         \  }}||z  }||z  |k    r|}||z  }n|}||z  }||z
  dz  }||z
  dz  }	|                     t          |          t          |	          t          ||z             t          |	|z             f          }
|
                    ||f          }|S )N   )sizecropr4   resize)r1   r3   r5   original_widthoriginal_heighttarget_aspect_ratiocrop_height
crop_widthlefttopimg_croppedimg_resizeds               r%   
crop_scalerD   X   s    &)h#NO%4'*===% #66

#
 #66Z'1,D[(A
-C((	TCHHc$"344c#:K6L6LM K $$k<%@AAK    encodingc                H   t          j        |           }t          j                            t          t          j        |                              }	 t          t          d          rt          j
        |          }n # t          $ r t          d|           Y nw xY w|J |S )Nexif_transposez0Failed to transpose image %s based on EXIF data.)r   extract_base64_datar.   r/   openr   base64	b64decodehasattrr
   rH   	Exceptionprint)rF   image_encodedr1   s      r%   decode_base64_to_imagerQ   o   s    $8BBM
)..!1-!@!@AA
B
BC
8-.. 	/)#..C 
 
 
>	
 	
 	
 	
 	


 ???Js   )A> >BB
np.ndarrayc                H    t          |           }t          j        |          S N)rQ   r   asarray)rF   r1   s     r%   decode_base64_to_image_arrayrV   ~   s    
 
*
*C:c??rE   c                B    t          |           }t          |||          S rT   )rQ   r0   )rF   r   r   r1   s       r%   decode_base64_to_filerX      s!    
 
*
*Cc9f---rE   image_arrayc                l   t                      5 }t          j                            t	          j        | t          j        d                    }|                    |d           |	                                }d d d            n# 1 swxY w Y   t          t          j        |          d          }d|z   S )NF)
force_copyJPEGutf-8data:image/jpeg;base64,)r   r.   r/   	fromarrayr   _convertr   uint8savegetvaluer   rK   	b64encode)rY   output_bytes	pil_image
bytes_data
base64_strs        r%   encode_image_array_to_base64ri      s    	 -lI''%k28NNN
 
	 	|V,,,!**,,
- - - - - - - - - - - - - - - V%j117;;J$z11s   A)BBBc                    t                      5 }|                     |d           |                                }d d d            n# 1 swxY w Y   t          t	          j        |          d          }d|z   S )Nr\   r]   r^   )r   rb   rc   r   rK   rd   )r   re   rg   rh   s       r%   encode_image_to_base64rk      s    	 -l

<(((!**,,
- - - - - - - - - - - - - - - V%j117;;J$z11s   +AA
A

image_file
str | Pathc                   t          t          |                     }t          | d          5 }|                                }d d d            n# 1 swxY w Y   t          t	          j        |          d          }d| d|z   S )Nrbr]   data:z;base64,)r   r   rJ   readrK   rd   )rl   	mime_typefrg   rh   s        r%   encode_image_file_to_base64rt      s    S__--I	j$		 1VVXX
              V%j117;;J&9&&&33s   AAAc                   t          |           } t          |           r/t          j        |           }|                                 |j        S t          |           5 }|                                }ddd           n# 1 swxY 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textrJ   rq   )rl   responsefilesvg_contents       r%   extract_svg_contentr}      s     ZJ
## 9Z((!!###}* 	&))++K	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   A>>BBpayloadImageData | None
image_modeULiteral['1', 'L', 'P', 'RGB', 'RGBA', 'CMYK', 'YCbCr', 'LAB', 'HSV', 'I', 'F'] | Nonec                   | | S | j         rj| j                             d          rP|dk    rt          | j                   S |dk    rt          | j                   S |dk    rt	          | j         ||          S | j        t          d          t          | j                  }| j        r=t          | j                  }|j	        }|j
                            dd          }|dv rd	}nd
}d}|                                dk    r$|dk    rt          |          S t          d          t          j                            |          }	|dk    r|d |	j        fv rt          |          S |	                                }
|
                    dd          dk    rSt+          t,          d          r>	 t-          j        |	          }	n(# t0          $ r t3          j        d| d           Y nw xY w|                                dk    rX|	Vt3          j                    5  t3          j        d           ||	                    |          }	d d d            n# 1 swxY w Y   t=          |	||||          S )Nrp   r   r   r   zImage path is None.. )jpgjpegr   r   r   svgz9SVG files are not supported as input images for this app.i     rH   zFailed to transpose image z based on EXIF data.gifignore)r   r   r   r   )url
startswithrQ   rV   rX   r$   r#   r   	orig_namestemsuffixreplacelowerr   r   r.   r/   rJ   modegetexifrw   rM   r
   rH   rN   warningswarncatch_warningssimplefilterconvertr&   )r~   r   r   r   r   	file_pathpr   r   r   exifs              r%   preprocess_imager      s    { Iw{--g66 I5==)'+666W__/<<<Z(iHHH|.///W\""I "##v!!#r**_$$F||~~:y>>!OPPP			"	"BzzdBG_<<9~~::<<DxxQ13C!D!D	X(,,BB 	X 	X 	XMVyVVVWWWWW	X||~~2>$&& 	, 	,!(+++%ZZ
++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 
   s$   'F< <"G! G!,I		IIvalue0np.ndarray | PIL.Image.Image | str | Path | Nonec                   | dS t          | t                    ri|                                                     d          rBt	          |           }t          t          |           j        dt          |                     S t          | ||          }t          |          
                                rt          |          j        nd}t          ||          S )z
    Parameters:
        value: Expects a `numpy.array`, `PIL.Image`, or `str` or `pathlib.Path` filepath to an image which is displayed.
    Returns:
        Returns the image as a `FileData` object.
    Nz.svgzdata:image/svg+xml,)r   r   r*   )r$   r   )r+   r   r   endswithr}   r   r   r   r   r0   exists)r   r   r   r|   savedr   s         r%   postprocess_imager      s     }t% 
%++--"8"8"@"@ 
)%005kk&:eK&8&8::
 
 
 	

 u	&AAAE$(KK$6$6$8$8BU  dI%95555rE   )r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   )r'   r(   r   r   r   r   )r1   r2   r3   r4   r5   r4   )rF   r   r   r2   )rF   r   r   rR   )rF   r   r   r   r   r   r   r   )rY   rR   r   r   )r   r2   r   r   )rl   rm   r   r   )r~   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   ))
__future__r   rK   r   ior   pathlibr   typingr   urllib.parser   rv   r   r   	PIL.Imager.   gradio_client.utilsr   r	   r
   gradior   gradio.data_classesr   gradio.exceptionsr   r/   initr&   r0   rD   rQ   rV   rX   ri   rk   rt   r}   r   r    rE   r%   <module>r      s   " " " " " "                                    > > > > > > > >       # # # # # # ) ) ) ) ) ) # # # # # # 	    !
 !
 !
 !
 !
J QW    :   .      
. . . . .
2 2 2 22 2 2 24 4 4 4   &; ; ; ;|6 6 6 6 6 6rE   