o
    7 h%                     @  s   d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZmZ erNdd	lmZ dd
lmZ ddlmZ G dd deZG dd deZdS )zImagePart and related objects.    )annotationsN)IOTYPE_CHECKINGAnycast)Image)Part)image_content_types)Emulazyproperty)PackURI)Package)Lengthc                      s   e Zd ZdZ	d)d* fddZed+ddZed,ddZed,ddZ	ed-ddZ
d.ddZed,d d!Zed/d"d#Zed0d%d&Zed/d'd(Z  ZS )1	ImagePartzrAn image part.

    An image part generally has a partname matching the regex `ppt/media/image[1-9][0-9]*.*`.
    Npartnamer   content_typestrpackager   blobbytesfilename
str | Nonec                   s&   t t| |||| || _|| _d S N)superr   __init___blob	_filename)selfr   r   r   r   r   	__class__ X/var/www/html/figdemos/bartoux_crm/venv/lib/python3.10/site-packages/pptx/parts/image.pyr      s   
zImagePart.__init__imager   returnc                 C  s   | | |j|j||j|jS )zcReturn new |ImagePart| instance containing `image`.

        `image` is an |Image| object.
        )next_image_partnameextr   r   r   )clsr   r"   r    r    r!   new(   s   
zImagePart.newc                 C  s   | j du rd| j S | j S )aV  The filename associated with this image.

        Either the filename of the original image or a generic name of the form `image.ext` where
        `ext` is appropriate to the image file format, e.g. `'jpg'`. An image created using a path
        will have that filename; one created with a file-like object will have a generic name.
        Nzimage.)r   r%   r   r    r    r!   desc6   s   
	zImagePart.descc                 C  s   | j jS )z0File-name extension for this image e.g. `'png'`.)r   r%   r(   r    r    r!   r%   C   s   zImagePart.extc                 C  s   t | j| jS )zAn |Image| object containing the image in this image part.

        Note this is a `pptx.image.Image` object, not a PIL Image.
        )r   r   r)   r(   r    r    r!   r"   H   s   zImagePart.image	scaled_cx
int | None	scaled_cytuple[int, int]c                 C  s   | j \}}|r|r||fS |r%|s%t|t| }tt|| }||fS |s=|r=t|t| }tt|| }||fS ||fS )a  Return scaled image dimensions in EMU based on the combination of parameters supplied.

        If `scaled_cx` and `scaled_cy` are both |None|, the native image size is returned. If
        neither `scaled_cx` nor `scaled_cy` is |None|, their values are returned unchanged. If a
        value is provided for either `scaled_cx` or `scaled_cy` and the other is |None|, the
        missing value is calculated such that the image's aspect ratio is preserved.
        )_native_sizefloatintround)r   r*   r,   Zimage_cxZimage_cyZscaling_factorr    r    r!   scaleP   s   
zImagePart.scalec                 C     t | j S )zThe 40-character SHA1 hash digest for the image binary of this image part.

        like: `"1be010ea47803b00e140b852765cdf84f491da47"`.
        hashlibsha1r   	hexdigestr(   r    r    r!   r6   j   s   zImagePart.sha1c                 C     t | j}|jS )zR(horz_dpi, vert_dpi) pair representing the dots-per-inch resolution of this image.)r   	from_blobr   dpir   r"   r    r    r!   _dpir      zImagePart._dpituple[Length, Length]c                 C  sH   d}| j \}}| j\}}|| | }|| | }tt|tt|fS )zA (width, height) 2-tuple representing the native dimensions of the image in EMU.

        Calculated based on the image DPI value, if present, assuming 72 dpi as a default.
        i )r<   _px_sizer
   r0   )r   ZEMU_PER_INCHZhorz_dpiZvert_dpiwidth_px	height_pxwidthheightr    r    r!   r.   x   s   

zImagePart._native_sizec                 C  r8   )zNA (width, height) 2-tuple representing the dimensions of this image in pixels.)r   r9   r   sizer;   r    r    r!   r?      r=   zImagePart._px_sizer   )
r   r   r   r   r   r   r   r   r   r   )r   r   r"   r   r#   r   r#   r   )r#   r   )r*   r+   r,   r+   r#   r-   r#   r-   )r#   r>   )__name__
__module____qualname____doc__r   classmethodr'   propertyr)   r%   r"   r2   r   r6   r<   r.   r?   __classcell__r    r    r   r!   r      s*    
r   c                      s   e Zd ZdZd% fddZed&d'd
dZed(ddZed)ddZ	e
d*ddZe
d+ddZe
d*ddZed,ddZe
d*ddZe
d+ddZed,d d!Ze
d-d#d$Z  ZS ).r   zIImmutable value object representing an image such as a JPEG, PNG, or GIF.r   r   r   r   c                   s   t t|   || _|| _d S r   )r   r   r   r   r   )r   r   r   r   r    r!   r      s   
zImage.__init__Nr#   c                 C  s
   | ||S )zCReturn a new |Image| object loaded from the image binary in `blob`.r    )r&   r   r   r    r    r!   r9         
zImage.from_blob
image_filestr | IO[bytes]c                 C  sz   t |tr%t|d}| }W d   n1 sw   Y  tj|}ntt|dr1|	d | }d}| 
||S )zReturn a new |Image| object loaded from `image_file`.

        `image_file` can be either a path (str) or a file-like object.
        rbNseekr   )
isinstancer   openreadospathbasenamecallablegetattrrR   r9   )r&   rO   fr   r   r    r    r!   	from_file   s   


zImage.from_filec                 C     | j S )z*The binary image bytestream of this image.)r   r(   r    r    r!   r      s   z
Image.blobr   c                 C  s
   t | j S )z-MIME-type of this image, e.g. `"image/jpeg"`.)r	   r%   r(   r    r    r!   r      rN   zImage.content_typer-   c                   s&   ddd d fdd}|| j d	 S )zA (horz_dpi, vert_dpi) 2-tuple specifying the dots-per-inch resolution of this image.

        A default value of (72, 72) is used if the dpi is not specified in the image file.
        r:   r   c              	   S  sN   zt tt| }|dk s|dkrd}W |S W |S  ttfy&   d}Y |S w )zReturn an integer dots-per-inch value corresponding to `dpi`.

            If `dpi` is |None|, a non-numeric type, less than 1 or greater than 2048, 72 is
            returned.
               i   H   )r0   r1   r/   	TypeError
ValueError)r:   int_dpir    r    r!   rb      s   zImage.dpi.<locals>.int_dpipil_dpituple[int, int] | Nonec                   s&   t | tr | d  | d fS dS )zReturn a (horz_dpi, vert_dpi) 2-tuple corresponding to `pil_dpi`.

            The value for the 'dpi' key in the `info` dict of a PIL image. If the 'dpi' key is not
            present or contains an invalid value, `(72, 72)` is returned.
            r   r^   )r_   r_   )rS   tuple)rc   rb   r    r!   normalize_pil_dpi   s   
z$Image.dpi.<locals>.normalize_pil_dpi   N)r:   r   )rc   rd   
_pil_props)r   rg   r    rf   r!   r:      s   

z	Image.dpic                 C  s@   ddddddd}| j }||vrd}t|| |f || S )	a  Canonical file extension for this image e.g. `'png'`.

        The returned extension is all lowercase and is the canonical extension for the content type
        of this image, regardless of what extension may have been used in its filename, if any.
        bmpgifjpgpngtiffwmf)BMPGIFJPEGPNGTIFFZWMFz7unsupported image format, expected one of: %s, got '%s')_formatra   keys)r   Zext_mapformattmplr    r    r!   r%      s   z	Image.extc                 C  r]   )zFilename from path used to load this image, if loaded from the filesystem.

        |None| if no filename was used in loading, such as when loaded from an in-memory stream.
        )r   r(   r    r    r!   r      s   zImage.filenamec                 C  r3   )z#SHA1 hash digest of the image blob.r4   r(   r    r    r!   r6      s   z
Image.sha1c                 C  
   | j d S )zLA (width, height) 2-tuple specifying the dimensions of this image in pixels.r^   ri   r(   r    r    r!   rD      rN   z
Image.sizec                 C  rz   )z/The PIL Image format of this image, e.g. 'PNG'.r   ri   r(   r    r    r!   rv     rN   zImage._format:tuple[str | None, tuple[int, int], tuple[int, int] | None]c                 C  sN   t | j}t|}|j}|j\}}td|j	d}|
  |||f|fS )zAtuple of image properties extracted from this image using Pillow.rd   r:   )ioBytesIOr   	PIL_ImagerT   rx   rD   r   infogetclose)r   streamZ	pil_imagerx   r@   rA   r:   r    r    r!   rj     s   


zImage._pil_props)r   r   r   r   r   )r   r   r   r   r#   r   )rO   rP   r#   r   )r#   r   rE   rF   )r#   r   )r#   r{   )rG   rH   rI   rJ   r   rK   r9   r\   rL   r   r   r   r:   r%   r   r6   rD   rv   rj   rM   r    r    r   r!   r      s2     r   )rJ   
__future__r   r5   r|   rV   typingr   r   r   r   PILr   r~   pptx.opc.packager   pptx.opc.specr	   	pptx.utilr
   r   pptx.opc.packurir   pptx.packager   r   r   objectr    r    r    r!   <module>   s     x