o
    pi                     @   s   d dl mZ d dlZd dlZd dlmZ d dlmZ eeej	j
edkrAejjjejjjejjjejjjejjjdZnejjejjejjejjejjdZdd Zd	d
 Zddeejj dedededejjf
ddZdS )    )ListN)version)Imagez9.1.0)linearbilinearbicubiclanczosnearestc                 C   s<   | d d  dd} |  dddd  } t| } | S )z/
    Convert a torch image to a PIL image.
       g      ?r         )clampcpupermutefloatnumpynumpy_to_pil)images r   W/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/utils/pil_utils.py	pt_to_pil   s   r   c                 C   sV   | j dkr	| d } | d  d} | jd dkr"dd | D }|S d	d | D }|S )
zD
    Convert a numpy image or a batch of images to a PIL image.
    r   )N.   uint8r   c                 S   s   g | ]}t j| d dqS )L)mode)r   	fromarraysqueeze.0imager   r   r   
<listcomp>.   s    z numpy_to_pil.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   r   r   r   r   r   r!   0   s    )ndimroundastypeshape)r   
pil_imagesr   r   r   r   %   s   
r   r   rowscolsresizereturnc           	         s   t | || ks
J  dur fdd| D } | d j\}}tjd|| || fd}t| D ]\}}|j||| | || | fd q/|S )zN
    Prepares a single grid of images. Useful for visualization purposes.
    Nc                    s   g | ]	}|   fqS r   r)   )r   imgr+   r   r   r!   <   s    z#make_image_grid.<locals>.<listcomp>r   RGB)size)box)lenr.   r   new	enumeratepaste)	r   r'   r(   r)   whgridir,   r   r+   r   make_image_grid5   s   $r8   )N)typingr   	PIL.ImagePILPIL.ImageOps	packagingr   r   parse__version__base_version
ResamplingBILINEARBICUBICLANCZOSNEARESTPIL_INTERPOLATIONLINEARr   r   intr8   r   r   r   r   <module>   s*    		
0