
    ,hK                        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 ddl	m
Z
 ddlmZmZmZmZ  G d d      Zdd	Z G d
 d      Zy)zProvides objects that can characterize image streams.

That characterization is as to content type and size, as a required step in including
them in a document.
    )annotationsN)IOTuple)UnrecognizedImageError)EmuInchesLengthlazypropertyc                  @    e Zd ZdZd fdZedd       Zedd       Zed        Z	edd       Z
ed        Zed        Zedd	       Zedd
       Zedd       Zedd       Zedd       Zedd       Z	 d	 	 	 	 	 ddZed        Ze	 d	 	 	 	 	 	 	 dd       Z xZS )ImagezgGraphical image stream such as JPEG, PNG, or GIF with properties and methods
    required by ImagePart.c                T    t         t        |           || _        || _        || _        y N)superr   __init___blob	_filename_image_header)selfblobfilenameimage_header	__class__s       R/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/docx/image/image.pyr   zImage.__init__   s&    eT#%
!)    c                P    t        j                  |      }| j                  ||      S )z`Return a new |Image| subclass instance parsed from the image binary contained
        in `blob`.)ioBytesIO_from_stream)clsr   streams      r   	from_blobzImage.from_blob   s%     D!--r   c                b   t        |t              r\|}t        |d      5 }|j                         }t	        j
                  |      }ddd       t        j                  j                  |      }n%|}|j                  d       |j                         }d}| j                  |      S # 1 sw Y   axY w)zReturn a new |Image| subclass instance loaded from the image file identified
        by `image_descriptor`, a path or file-like object.rbNr   )
isinstancestropenreadr   r   ospathbasenameseekr   )r   image_descriptorr)   fr   r    r   s          r   	from_filezImage.from_file#   s     &,#DdD! *QvvxD)* ww''-H%FKKN;;=DHh77* *s   &B%%B.c                    | j                   S )zThe bytes of the image 'file'.)r   r   s    r   r   z
Image.blob4   s     zzr   c                .    | j                   j                  S )zIMIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image.)r   content_typer0   s    r   r2   zImage.content_type9   s     !!...r   c                `    t         j                  j                  | j                        d   dd S )a  The file extension for the image.

        If an actual one is available from a load filename it is used. Otherwise a
        canonical extension is assigned based on the content type. Does not contain the
        leading period, e.g. 'jpg', not '.jpg'.
           N)r(   r)   splitextr   r0   s    r   extz	Image.ext>   s)     ww/21266r   c                    | j                   S )zpOriginal image file name, if loaded from disk, or a generic filename if
        loaded from an anonymous stream.)r   r0   s    r   r   zImage.filenameH   s     ~~r   c                .    | j                   j                  S z,The horizontal pixel dimension of the image.)r   px_widthr0   s    r   r:   zImage.px_widthN   s     !!***r   c                .    | j                   j                  S z*The vertical pixel dimension of the image.)r   	px_heightr0   s    r   r=   zImage.px_heightS   s     !!+++r   c                .    | j                   j                  S zInteger dots per inch for the width of this image.

        Defaults to 72 when not present in the file, as is often the case.
        )r   horz_dpir0   s    r   r@   zImage.horz_dpiX        !!***r   c                .    | j                   j                  S zInteger dots per inch for the height of this image.

        Defaults to 72 when not present in the file, as is often the case.
        )r   vert_dpir0   s    r   rD   zImage.vert_dpi`   rA   r   c                F    t        | j                  | j                  z        S )z}A |Length| value representing the native width of the image, calculated from
        the values of `px_width` and `horz_dpi`.)r   r:   r@   r0   s    r   widthzImage.widthh   s     dmmdmm344r   c                F    t        | j                  | j                  z        S )zA |Length| value representing the native height of the image, calculated from
        the values of `px_height` and `vert_dpi`.)r   r=   rD   r0   s    r   heightzImage.heightn   s     dnnt}}455r   c                Z   ||| j                   | j                  fS |=|J t        |      t        | j                        z  }t        | j                   |z        }|9t        |      t        | j                         z  }t        | j                  |z        }t	        |      t	        |      fS )a?  (cx, cy) pair representing scaled dimensions of this image.

        The native dimensions of the image are scaled by applying the following rules to
        the `width` and `height` arguments.

        * If both `width` and `height` are specified, the return value is (`width`,
        `height`); no scaling is performed.
        * If only one is specified, it is used to compute a scaling factor that is then
        applied to the unspecified dimension, preserving the aspect ratio of the image.
        * If both `width` and `height` are |None|, the native dimensions are returned.

        The native dimensions are calculated using the dots-per-inch (dpi) value
        embedded in the image, defaulting to 72 dpi if no value is specified, as is
        often the case. The returned values are both |Length| objects.
        )rF   rH   floatroundr   )r   rF   rH   scaling_factors       r   scaled_dimensionszImage.scaled_dimensionst   s    $ =V^::t{{**=%%"6]U4;;-??N$**~56E>"5\E$**,==N4;;78F5z3v;&&r   c                \    t        j                  | j                        j                         S )z#SHA1 hash digest of the image blob.)hashlibsha1r   	hexdigestr0   s    r   rP   z
Image.sha1   s      ||DJJ'1133r   c                N    t        |      }|d|j                  z  } | |||      S )zhReturn an instance of the |Image| subclass corresponding to the format of the
        image in `stream`.zimage.%s)_ImageHeaderFactorydefault_ext)r   r    r   r   r   s        r   r   zImage._from_stream   s3     +62!L$<$<<H4<00r   )r   bytesr   r%   r   BaseImageHeader)r   rU   returnr   )r,   zstr | IO[bytes]rW   r%   )rW   int)rW   r   )NN)rF   int | Length | NonerH   rZ   rW   zTuple[Length, Length]r   )r    	IO[bytes]r   rU   r   z
str | NonerW   r   )__name__
__module____qualname____doc__r   classmethodr!   r.   propertyr   r2   r
   r6   r   r:   r=   r@   rD   rF   rH   rM   rP   r   __classcell__)r   s   @r   r   r      su   * . . 8 8    / / 7 7  
 + + , , + + + + 5 5
 6 6 PT'('9L'	'@ 4 4 
  $	11 1 	1
 
1 1r   r   c                    ddl m} dd} ||       }|D ]1  \  }}}|t        |      z   }||| }||k(  s |j                  |       c S  t        )zRA |BaseImageHeader| subclass instance that can parse headers of image in `stream`.r   )
SIGNATURESc                F    | j                  d       | j                  d      S )Nr       )r+   r'   )r    s    r   read_32z$_ImageHeaderFactory.<locals>.read_32   s    A{{2r   r    r[   )
docx.imagerd   lenfrom_streamr   )	r    rd   rg   headerr   offsetsignature_bytesendfound_bytess	            r   rS   rS      se    % V_F(2 +$V_s?++VC(/)??6**	+
 ! r   c                  |    e Zd ZdZd
dZedd       Zedd       Zed        Zed        Z	ed        Z
ed        Zy	)rV   z>Base class for image header subclasses like |Jpeg| and |Tiff|.c                <    || _         || _        || _        || _        y r   )	_px_width
_px_height	_horz_dpi	_vert_dpi)r   r:   r=   r@   rD   s        r   r   zBaseImageHeader.__init__   s    !#!!r   c                    d}t        |      )zDAbstract property definition, must be implemented by all subclasses.zNcontent_type property must be implemented by all subclasses of BaseImageHeaderNotImplementedError)r   msgs     r   r2   zBaseImageHeader.content_type   s     b!#&&r   c                    t        d      )zDefault filename extension for images of this type.

        An abstract property definition, must be implemented by all subclasses.
        zMdefault_ext property must be implemented by all subclasses of BaseImageHeaderrx   r0   s    r   rT   zBaseImageHeader.default_ext   s     "^
 	
r   c                    | j                   S r9   )rs   r0   s    r   r:   zBaseImageHeader.px_width   s     ~~r   c                    | j                   S r<   )rt   r0   s    r   r=   zBaseImageHeader.px_height   s     r   c                    | j                   S r?   )ru   r0   s    r   r@   zBaseImageHeader.horz_dpi        ~~r   c                    | j                   S rC   )rv   r0   s    r   rD   zBaseImageHeader.vert_dpi   r   r   N)r:   rY   r=   rY   r@   rY   rD   rY   rX   )r\   r]   r^   r_   r   ra   r2   rT   r:   r=   r@   rD    r   r   rV   rV      s    H" ' '
 
 
        r   rV   rh   )r_   
__future__r   rO   r   r(   typingr   r   docx.image.exceptionsr   docx.sharedr   r   r	   r
   r   rS   rV   r   r   r   <module>r      s@    #  	 	  8 9 9S1 S1l!"1 1r   