;iEdZddlmZddlZddlZddlZddlmZmZddl m Z ddl m Z m Z mZmZGddZdd ZGd d ZdS)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)IOTuple)UnrecognizedImageError)EmuInchesLength lazypropertyc|eZdZdZd%fd Zed&d Zed'd ZedZ ed(dZ e dZ edZ ed)dZed)dZed)dZed)dZed*dZed*dZ d+d,dZe d Ze d-d.d$ZxZS)/ImagezgGraphical image stream such as JPEG, PNG, or GIF with properties and methods required by ImagePart.blobbytesfilenamestr image_headerBaseImageHeaderctt|||_||_||_dSN)superr __init___blob _filename _image_header)selfr rr __class__s DC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\docx/image/image.pyrzImage.__init__s; eT##%%% !)returncVtj|}|||S)z`Return a new |Image| subclass instance parsed from the image binary contained in `blob`.)ioBytesIO _from_stream)clsr streams r from_blobzImage.from_blobs)D!!---rimage_descriptorstr | IO[bytes]ct|trr|}t|d5}|}t j|}dddn #1swxYwYt j|}n-|}| d|}d}| |||S)zReturn a new |Image| subclass instance loaded from the image file identified by `image_descriptor`, a path or file-like object.rbNr) isinstanceropenreadr r!ospathbasenameseekr")r#r&r.fr r$rs r from_filezImage.from_file#s & , , #DdD!! *QvvxxD)) * * * * * * * * * * * * * * *w''--HH%F KKNNN;;==DHh777s)AA!$A!c|jS)zThe bytes of the image 'file'.)rrs rr z Image.blob4s zrc|jjS)zIMIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image.)r content_typer4s rr6zImage.content_type9s!..rcftj|jdddS)aThe 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.splitextrr4s rextz Image.ext>s*w//212266rc|jS)zpOriginal image file name, if loaded from disk, or a generic filename if loaded from an anonymous stream.)rr4s rrzImage.filenameHs ~rintc|jjSz,The horizontal pixel dimension of the image.)rpx_widthr4s rr?zImage.px_widthNs!**rc|jjSz*The vertical pixel dimension of the image.)r px_heightr4s rrBzImage.px_heightSs!++rc|jjSzInteger dots per inch for the width of this image. Defaults to 72 when not present in the file, as is often the case. )rhorz_dpir4s rrEzImage.horz_dpiX !**rc|jjSzInteger dots per inch for the height of this image. Defaults to 72 when not present in the file, as is often the case. )rvert_dpir4s rrIzImage.vert_dpi`rFrrc:t|j|jz S)z}A |Length| value representing the native width of the image, calculated from the values of `px_width` and `horz_dpi`.)rr?rEr4s rwidthz Image.widthhsdmdm3444rc:t|j|jz S)zA |Length| value representing the native height of the image, calculated from the values of `px_height` and `vert_dpi`.)rrBrIr4s rheightz Image.heightnsdnt}4555rNrKint | Length | NonerMTuple[Length, Length]c^|||j|jfS|?|Jt|t|jz }t|j|z}|;t|t|jz }t|j|z}t |t |fS)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. )rKrMfloatroundr)rrKrMscaling_factors rscaled_dimensionszImage.scaled_dimensionsts$ =V^:t{* * =%%%"6]]U4;-?-??N$*~566E >"5\\E$*,=,==N4;788F5zz3v;;&&rcXtj|jS)z#SHA1 hash digest of the image blob.)hashlibsha1r hexdigestr4s rrWz Image.sha1s"|DJ''11333rr$ IO[bytes] str | NonecRt|}| d|jz}||||S)zhReturn an instance of the |Image| subclass corresponding to the format of the image in `stream`.Nzimage.%s)_ImageHeaderFactory default_ext)r#r$r rrs rr"zImage._from_streams8+622  !L$<.read_32s! A{{2rNr$rY) docx.imagerglen from_streamr) r$rgrjheaderr#offsetsignature_bytesend found_bytess rr\r\s%%%%%%WV__F(2++$V_s?+++VCZ( / ) )??6** * * * *  rceZdZdZddZedd Zedd Zed Zed Z edZ edZ dS)rz>Base class for image header subclasses like |Jpeg| and |Tiff|.r?r<rBrErIc>||_||_||_||_dSr) _px_width _px_height _horz_dpi _vert_dpi)rr?rBrErIs rrzBaseImageHeader.__init__s"!#!!rrrc$d}t|)zDAbstract property definition, must be implemented by all subclasses.zNcontent_type property must be implemented by all subclasses of BaseImageHeaderNotImplementedError)rmsgs rr6zBaseImageHeader.content_types_!#&&&rc td)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 BaseImageHeaderr{r4s rr]zBaseImageHeader.default_exts " [   rc|jSr>)rvr4s rr?zBaseImageHeader.px_widths ~rc|jSrA)rwr4s rrBzBaseImageHeader.px_heights rc|jSrD)rxr4s rrEzBaseImageHeader.horz_dpi ~rc|jSrH)ryr4s rrIzBaseImageHeader.vert_dpirrN)r?r<rBr<rEr<rIr<r^) r_r`rarbrrdr6r]r?rBrErIrrrrsHH"""" '''X'    X XXXXrrrk)rb __future__rrVr r-typingrrdocx.image.exceptionsr docx.sharedrrr r r r\rrrrrs #""""" 888888999999999999S1S1S1S1S1S1S1S1l!!!!"1111111111r