U
    cqeê	  ã                   @  sT   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 G dd„ deƒZdS )	z7The proxy class for an image part, and related objects.é    )ÚannotationsN)ÚImage)ÚPart)ÚEmuÚInchesc                      sˆ   e Zd ZdZddddddœ‡ fdd„Zed	d
„ ƒZedd„ ƒZedd„ ƒZe	dd„ ƒZ
eddœdd„ƒZe	dd„ ƒZedd„ ƒZ‡  ZS )Ú	ImagePartzlAn image part.

    Corresponds to the target part of a relationship with type RELATIONSHIP_TYPE.IMAGE.
    NÚstrÚbyteszImage | None)ÚpartnameÚcontent_typeÚblobÚimagec                   s   t t| ƒ |||¡ || _d S ©N)Úsuperr   Ú__init__Ú_image)Úselfr
   r   r   r   ©Ú	__class__© ú4/tmp/pip-unpacked-wheel-gx5hpqp3/docx/parts/image.pyr      s    zImagePart.__init__c                 C  s    | j j}| j j}|| }t|ƒS )zkNative width of this image, calculated from its width in pixels and
        horizontal dots per inch (dpi).)r   Úpx_widthÚhorz_dpir   )r   r   r   Zwidth_in_inchesr   r   r   Ú
default_cx   s    zImagePart.default_cxc                 C  s$   | j j}| j j}d| | }t|ƒS )zkNative height of this image, calculated from its height in pixels and
        vertical dots per inch (dpi).iàó )r   Ú	px_heightr   r   )r   r   r   Zheight_in_emur   r   r   Ú
default_cy!   s    zImagePart.default_cyc                 C  s   | j dk	r| j jS d| jj S )a?  Filename from which this image part was originally created.

        A generic name, e.g. 'image.png', is substituted if no name is available, for
        example when the image was loaded from an unnamed stream. In that case a default
        extension is applied based on the detected MIME type of the image.
        Nzimage.%s)r   Úfilenamer
   Úext©r   r   r   r   r   *   s    
zImagePart.filenamec                 C  s   t ||j|j|ƒS )zZReturn an |ImagePart| instance newly created from `image` and assigned
        `partname`.)r   r   r   )Úclsr   r
   r   r   r   Ú
from_image6   s    zImagePart.from_imager   )Úreturnc                 C  s   | j d krt | j¡| _ | j S r   )r   r   Z	from_blobr   r   r   r   r   r   <   s    
zImagePart.imagec                 C  s   | |||ƒS )zCalled by ``docx.opc.package.PartFactory`` to load an image part from a
        package being opened by ``Document(...)`` call.r   )r   r
   r   r   Úpackager   r   r   ÚloadB   s    zImagePart.loadc                 C  s   t  | j¡ ¡ S )z0SHA1 hash digest of the blob of this image part.)ÚhashlibÚsha1Z_blobÚ	hexdigestr   r   r   r   r%   H   s    zImagePart.sha1)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   Úclassmethodr    r   r#   r%   Ú__classcell__r   r   r   r   r      s"    ÿ




r   )r*   Ú
__future__r   r$   Zdocx.image.imager   Zdocx.opc.partr   Zdocx.sharedr   r   r   r   r   r   r   Ú<module>   s   