o
    pi                  	   @   s   d dl Z d dlZd dlmZmZmZmZ d dlZd dl	Zd dl
Z
ddlmZmZ 	ddeeejjf deeejjgejjf  dejjfdd	Z	dd
edeeeejj geejj f  deejj fddZdS )    N)CallableListOptionalUnion   )BACKENDS_MAPPINGis_opencv_availableimageconvert_methodreturnc                 C   s   t | tr1| ds| drtjtj| ddj} n#t	j
| r)tj| } ntd|  dt | tjjr;| } ntdtj| } |durO|| } | S | d	} | S )
a  
    Loads `image` to a PIL Image.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.
        convert_method (Callable[[PIL.Image.Image], PIL.Image.Image], *optional*):
            A conversion method to apply to the image after loading it. When set to `None` the image will be converted
            "RGB".

    Returns:
        `PIL.Image.Image`:
            A PIL Image.
    http://https://TstreamIIncorrect path or URL. URLs must start with `http://` or `https://`, and  is not a valid path.zgIncorrect format used for the image. Should be a URL linking to an image, a local path, or a PIL image.NRGB)
isinstancestr
startswithPILImageopenrequestsgetrawospathisfile
ValueErrorImageOpsexif_transposeconvert)r	   r
    r#   [/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/utils/loading_utils.py
load_image   s&   


r%   videoc                 C   s  |  dp	|  d}tj| }d}|s|std|  d|rVtj| ddj}tj	tj
| d dd	j}d}t|d
}||  W d   n1 sOw   Y  |} g }| drtj| }	z	 ||	  |	|	 d  qe ty~   Y n=w t rddl}
nttd d d|
| }| \}}|r|
||
j}|tj| | \}}|s|   |rt!| |dur||}|S )a  
    Loads `video` to a list of PIL Image.

    Args:
        video (`str`):
            A URL or Path to a video to convert to a list of PIL Image format.
        convert_method (Callable[[List[PIL.Image.Image]], List[PIL.Image.Image]], *optional*):
            A conversion method to apply to the video after loading it. When set to `None` the images will be converted
            to "RGB".

    Returns:
        `List[PIL.Image.Image]`:
            The video as a list of PIL images.
    r   r   Fr   r   Tr   r   )suffixdeletewbNz.gifr   opencv
load_video)"r   r   r   r   r   r   r   r   tempfileNamedTemporaryFilesplitextnamer   writereadendswithr   r   appendcopyseektellEOFErrorr   cv2ImportErrorr   formatVideoCapturecvtColorCOLOR_BGR2RGB	fromarrayreleaseremove)r&   r
   is_urlis_filewas_tempfile_created
video_data
video_pathf
pil_imagesgifr8   video_capturesuccessframer#   r#   r$   r+   7   sT   




r+   )N)r   r,   typingr   r   r   r   	PIL.Imager   PIL.ImageOpsr   import_utilsr   r   r   r   r%   r+   r#   r#   r#   r$   <module>   s.    
- 
