o
    Gi                     @   s  d dl Z d dlZd dlmZmZ d dlmZmZ d dlZ	d dl
Z	d dlZddlmZ ddlmZmZ 	ddee	jjB dee	jjge	jjf dB d	e	jjfd
dZ	ddedeee	jj gee	jj f dB d	ee	jj fddZded	eeef fddZdefddZdS )    N)AnyCallable)unquoteurlparse   )DIFFUSERS_REQUEST_TIMEOUT)BACKENDS_MAPPINGis_imageio_availableimageconvert_methodreturnc                 C   s   t | tr2| ds| drtjtj| dtdj	} n#t
j| r*tj| } ntd|  dt | tjjr<| } ntdtj| } |durP|| } | 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)streamtimeoutIIncorrect 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getr   rawospathisfile
ValueErrorImageOpsexif_transposeconvert)r
   r    r$   Q/home/ubuntu/.local/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|r|tj| dd}|jdkr4td	|j t| }tj	t
|j}tj|d
 pJd}tj|ddj}	d}|jdd}
t|	d}|
D ]}|| qcW d   n1 suw   Y  |	} g }| drtj| }z	 ||  || d
  q ty   Y nKw t rddl}nttd d
 dz|jj !  W n t"y   t"dw |#| }|D ]}|tj$| qW d   n1 sw   Y  |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   T)r      z'Failed to download video. Status code: r   z.mp4)suffixdeletei    )
chunk_sizewbNz.gifr   imageio
load_videozf`Unable to find an ffmpeg installation on your machine. Please install via `pip install imageio-ffmpeg)&r   r   r   r   r    r   r   status_coder   basenamer   splitexttempfileNamedTemporaryFilenameiter_contentr   writeendswithr   r   appendcopyseektellEOFErrorr	   r-   ImportErrorr   formatpluginsffmpegget_exeAttributeError
get_reader	fromarrayremove)r'   r   is_urlis_filewas_tempfile_createdresponse
parsed_url	file_namer)   
video_path
video_datafchunk
pil_imagesgifr-   readerframer$   r$   r%   r.   9   sl   




r.   tensor_namec                 C   s^   d|v r+| d}|d d D ]}t| |}|d u r$t|  d| d|} q|d }| |fS )N.z has no attribute )splitgetattrr    )modulerT   splitsrW   
new_moduler$   r$   r%   get_module_from_name   s   

r\   module_pathc                 C   s@   | }| d}|D ]}| rt|}|| }q	t||}q	|S )NrU   )rW   isdigitintrX   )root_moduler]   currentpartspartidxr$   r$   r%   get_submodule_by_name   s   

re   )N)r   r2   typingr   r   urllib.parser   r   	PIL.Imager   PIL.ImageOpsr   	constantsr   import_utilsr   r	   r   r   r&   listr.   tupler\   re   r$   r$   r$   r%   <module>   s6    

- 

U