o
    Si                     @   s^   d dl mZmZ d dlmZ d dlmZmZ d dlZ	d dl
mZmZmZ eG dd dZdS )    )asdict	dataclass)Path)OptionalTupleN)Pathlikefastcopyis_module_availablec                   @   s   e Zd ZU dZeed< eed< eed< eed< eed< edeeef fdd	Z	ede
fd
dZdefddZededd fddZed!ddZd"de
fddZdedd fddZd!ddZdd Zd#dd ZdS )$Imagea6  
    The Image manifest describes an image that is stored somewhere: it might be
    in a file, in memory, in the cloud, etc.
    Image helps abstract away from the actual storage mechanism and location by
    providing a method called :meth:`.Image.load`.

    Image manifest can be easily created by calling
    :meth:`lhotse.image.io.PillowWriter.store_image`, for example::

        >>> from lhotse.image.io import PillowWriter
        >>> with PillowWriter('images/') as writer:
        ...     manifest = writer.store_image('image-1', 'path/to/image.jpg')
    storage_typestorage_pathstorage_keywidthheightreturnc                 C   s   | j | jfS )z1Return the shape of the image as (height, width).)r   r   self r   F/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/image/image.pyshape*   s   zImage.shapec                 C   s   ddl m} || jS )z'Check if the image is stored in memory.r   )is_in_memory)lhotse.image.ior   r   )r   r   r   r   r   r   /   s   
zImage.is_in_memoryc                 C   s   t | S )z(Convert to dictionary for serialization.)r   r   r   r   r   to_dict7   s   zImage.to_dictdatac                 C   s.   d|v rd|v rd|vrd|d< | di |S )z1Create an Image from a dictionary representation.r   r   r   Nr   r   )clsr   r   r   r   	from_dict;   s
   zImage.from_dictc                 C   s   ddl m} | d|S )Nr   )PillowInMemoryWriter )r   r   store_image)r   r   r   r   r   r   from_pillowF   s   zImage.from_pillowFas_pil_imagec                 C   s@   t dsJ dddlm} || j| j}|j| j|d}|S )a'  
        Load the image from the underlying storage.

        :param as_numpy: If True (default), returns a numpy array with shape (height, width, channels).
                         If False, returns a PIL Image object.
        :return: Loaded image as numpy array or PIL Image object.
        PILz8In order to load images, please run 'pip install pillow'r   )
get_reader)r    )r	   r   r"   r   r   readr   )r   r    r"   storageimg_datar   r   r   loadL   s   z
Image.loadpathc                 C   s   t | tt|| j dS )zt
        Return a copy of the image with ``path`` added as a prefix
        to the ``storage_path`` member.
        )r   )r   strr   r   )r   r'   r   r   r   with_path_prefix`   s   zImage.with_path_prefixc                 C   sP   ddl m} | jdv r| S |  }|d }|d|}t|j|d| j| jdS )z>
        Return a copy of the image stored in memory.
        r   )get_memory_writerpillow_memoryr   )r   r   r   r   r   )	r   r*   r   r&   writer
   namer   r   )r   r*   imgwriterr   r   r   r   move_to_memoryg   s   

zImage.move_to_memoryc                 C   s>   d| j  d| j dt| jtr| jnd d| j d| j dS )NzImage(storage_type='z', storage_path='z', storage_key='z<binary-data>z	', width=z	, height=))r   r   
isinstancer   r(   r   r   r   r   r   r   __repr__|   s   zImage.__repr__Nc                 K   sX   t dstdddlm} |  }|du r| \}}|j|fi | |d |S )a/  
        Display the image using matplotlib.

        :param ax: Optional matplotlib Axes to plot on. If None, a new figure and axes will be created.
        :param kwargs: Additional keyword arguments passed to `ax.imshow()`.
        :return: The matplotlib Axes object with the plotted image.
        
matplotlibz;To use Image.plot(), please 'pip install matplotlib' first.r   Noff)r	   ImportErrormatplotlib.pyplotpyplotr&   subplotsimshowaxis)r   axkwargspltr%   figr   r   r   plot   s   
z
Image.plot)r   r
   )F)N)__name__
__module____qualname____doc__r(   __annotations__intpropertyr   r   boolr   dictr   classmethodr   r   r&   r   r)   r0   r3   r@   r   r   r   r   r
   
   s*   
 

r
   )dataclassesr   r   pathlibr   typingr   r   numpynplhotse.utilsr   r   r	   r
   r   r   r   r   <module>   s    