)EjlxddlmZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z m Z ddl mZmZddlmZmZdZerddlmZejeZd Z d ZdZ d d d dddZ d>dZd?dZGdde ZGddejZ Gddej!Z"Gd d!e ej#"Z$Gd#d$Z%d@dAd-Z& dBdCd2Z'dDd5Z(Gd6d7Z)Gd8d9Z*Gd:d;e*Z+Gd<d=e*Z,dS)E) annotationsN)IOAny NamedTuplecast)ExifTagsImage) DeferredErroris_pathF)StrOrBytesPathiizimage buffer overrun errorzdecoding errorz unknown errorzbad configurationzout of memory error)ierrorintencoderboolreturnOSErrorc tj|}n*#t$rt|}YnwxYw|s |rdndd|}|d|rdnddz }t |S)Nrdecoderz error z when writingreadingz image file)r coregetcodecstatusAttributeErrorERRORSgetr)rrmsgs ?C:\PYTHON\MyICR_Suite\python\Lib\site-packages\PIL/ImageFile.py _get_oserrorr#Vs j''..    jj E%49DDUDD D7IIi D D DDC 3<<s"$A A t_Tilec|dS)N)r$s r" _tilesortr)as Q4Kc@eZdZUded<ded<dZded<dZd ed <dS) r%str codec_name tuple[int, int, int, int] | NoneextentsrroffsetNztuple[Any, ...] | str | Noneargs)__name__ __module__ __qualname____annotations__r0r1r(r*r"r%r%fsEOOO----FOOOO)-D------r*ceZdZdZ d"d#fd Zd$d Zd%d Zd$d Zd&dZd$fd Z d'dZ d(dZ d)fd Z d*fd Z d$dZd+dZd$dZd$dZd,d!ZxZS)- ImageFilez*Base class for image file format handlers.NfpStrOrBytesPath | IO[bytes]filenamestr | bytes | NonerNonectd|_d|_g|_ d|_d|_t|_||t|r6t|d|_ tj ||_d|_n7t!t"t$||_ ||nd|_d|_ |t)|t*r+|x}r| |n@#t.t0t2t4t6jf$r}t;||d}~wwxYw|jr"|jddks|jddkrd}t;|dS#t@$r"|jr|j !wxYw) Nrrr(rbTFznot identified by this driver)"super__init__ _min_framecustom_mimetypetilereadonly decoderconfigMAXBLOCKdecodermaxblockr openr8osfspathr: _exclusive_fprrbytes_open isinstance StubImageFile_load IndexError TypeErrorKeyErrorEOFErrorstructr SyntaxErrormodesize BaseExceptionclose)selfr8r:loadervr! __class__s r"rAzImageFile.__init__us +/!# * .0' ! + 2;; '2tnnDGIbMMDM!%D  2e9b))DG(0(g|]}||fSr() _get_ifd_dict).0 subifd_offsetexifs r" z.ImageFile.get_child_images..s<%'' 66 Fr* _exif_offsetrr)TiffImagePlugin)getexifr BaseSubIFDsrOtupleget_ifdIFDIFD1r JpegIFOffset_infoappendnextr8tellriseekJpegIFByteCountrreadioBytesIOr rIr?r} TiffImageFile _frame_pos_seekload)r\ child_imagesifdssubifd_offsetsifd1r0ifd ifd_offsetcurrent_offsetr8thumbnail_offsetlengthdataimr}rzs @r"get_child_imageszImageFile.get_child_imagess ||~~ = D ( (!(-"78N !.%887&4%6N)7||HL-..  1DHHX]788 1:))) KKtz/ 0 0 0# ( (OC7&&&!W\\^^N~'B#&778=+E#F#F #/$na(H(HH$GLL!1222 WWX]%BCCF%fc222227<<//DD))BB (2------#+ 511+&0LBMHHQKKK ##B''' ( ( ( ( ( ( ( ( ( ( ( ( ( ( (  7&&& GLL s*A$II !I str | Nonec|jr|jS|j6tj|jSdSra)rCformatr MIMEr upperrbs r"get_format_mimetypezImageFile.get_format_mimetype sE   (' ' ; ":>>$+"3"3"5"566 6tr* list[Any]cVt|jgzSra)r@ __getstate__r:)r\r_s r"rzImageFile.__getstate__s"ww##%%77r*statecg|_t|dkr |d|_t|dS)N)rDlenr:r@ __setstate__)r\rr_s r"rzImageFile.__setstate__sB u::>>!!HDM U#####r*cb|jr |jr|jd|_dS)zCheck file integrityN)rLr8r[rbs r"verifyzImageFile.verifys2   $'  GMMOOOr*Image.core.PixelAccess | Nonec |js|jd}t|tj|}|js|Sd|_|jot|jdk}|jJd}t|dr |j }d}n |jj }t|dr |j }d}n |jj }|r|jd\}}} } t| tr| ddf} |dkr~t| t rht| d krT| d|jkrB| dtjvr-| dkrd }t'| ddl} t+|j5} | | d| j |_dddn #1swxYwY| |jd| dzz|jkrd }t|tj|j|j|| | |_d}|jr d|j_n$#t<tt>f$r d|_YnwxYw| d } |js|j!tDtG|dd}dtIj%|jdD|_tM|jD]\}\}}} } || tj'|j|| |j(} |)|j||j*r6|+|j|,dd} n|} |j-}|dzt|jkr |j|dzj.}|| kr|| z } ||}n<#t^t`j1f$r#}tdrYd}~nud}t||d}~wwxYw|s*tdrnQdt|d}t|||z}|,|\}} |dkrn ||d}|3#|3wxYwg|_||_4|5|j6r |j7r|j8d|_|jstds| dkrts| dtj|S)z"Load image data based on tile listNzcannot load this imagerr load_readF load_seekrawzTile offset cannot be negative)accesszbuffer is not large enoughrkey tile_prefixr*c>g|]\}}t|dS)r)list)rx_tiless r"r{z"ImageFile.load..os6AuU Br*c0|d|d|dfS)Nrrrr()rDs r"z ImageFile.load..rsT!Wd1gtAw,Gr*Tzimage file is truncatedzimage file is truncated (z bytes not processed)r):rD_imrr rmapr:rr8hasattrrrrrrOr,rrX _MAPMODESrjmmaprIfileno ACCESS_READrYr map_bufferrpalettedirtyr ImportError load_preparesortr)ri itertoolsgroupby enumerate _getdecoderrFsetimagepulls_fdsetfddecoderHr0rRrVrLOAD_TRUNCATED_IMAGEScleanuprEload_endrL!_close_exclusive_fp_after_loadingr[r#)r\r!pixeluse_mmaprErr decoder_namer/r0r1rr8err_codeprefixirb read_bytes next_offsetsens r"rzImageFile.load#sy TX-*C#,,    &&y L%)=8S^^q%8w""" 4 % % >DHH7DHH7>##6#6q#9"&)-)=J 1us49~~55.2iA.>.E #.#7#71$(D$4$4$. #=>>>#8!>$)EEEE*CC*1#,,A$= >$%7#8!7$))J,/FF)J)J)J%(+2#,,$6 !AA*1..*;*;KAx 1uu % !!""A=&BOO%%%%GOO%%%%       $"H  GMMOOOx 8 5 8(Q,,x777 7{%%%su$I<5F=1 I=GIGBII65I6>BR O('R(P!>PR PP!!ARR+c|j/tj|j|j|_|jdkr!tj|dSdS)NP)rr rnewrXrYrrrbs r"rzImageFile.load_preparesV 8 jnnTY ::DG 9   K  T " " " " "  r*cdSrar(rbs r"rzImageFile.load_end r*framerrc||jks3t|dr|j-|t|d|jzkrd}t |||kS)N _n_framesn_framesz attempt to seek outside sequence)rBrrrirUr)r\rr!s r" _seek_checkzImageFile._seek_checkso DO # #T;// $59N4JWT:66HHH5C3-- yy{{e##r*ra)r8r9r:r;rr<rr<)rr7r1rlrr<)rrt)rr)rr)rrrr<rr)rrrr)r2r3r4__doc__rArNrerkror[rrrrrrrrr __classcell__)r_s@r"r7r7rs44NR6666666p     (3333j888888$$$$$$ N&N&N&N&`####     $ $ $ $ $ $ $ $r*r7c8eZdZd dZejd dZdS) StubHandlerrrPrr<cdSrar(r\rs r"rIzStubHandler.openrcr* Image.ImagecdSrar(rs r"rzStubHandler.loadrr*N)rrPrr<)rrPrr)r2r3r4rIabcabstractmethodrr(r*r"rrsL           r*rc^eZdZdZejd dZd dZejd dZd S) rPz Base class for stub image loaders. A stub loader is an image loader that can identify files of a certain format, but relies on external code to load the file. rr<cdSrar(rbs r"rNzStubImageFile._openrr*rc|}|d|jd}t|||}|J|j|_|j|_|S)Nzcannot find loader for this z file)rQrrrr___dict__)r\r]r!images r"rzStubImageFile.loadso >CCCCC#,,  D!!    zz||r*StubHandler | NonecdS)z (Hook) Find actual image loader.Nr(rbs r"rQzStubImageFile._loads  r*Nrr)rr) r2r3r4rrrrNrrQr(r*r"rPrPsz                r*rP) metaclasscreZdZUdZdZdZded<dZded<dZded<d Z d Z dd Z ddZ ddZ ddZddZdS)Parserzj Incremental image parser. This class implements the standard feed/close consumer interface. NzImage.Image | Nonerz bytes | Nonerz,Image.core.ImagingDecoder | PyDecoder | Nonerrrr<c(|j JddS)z (Consumer) Reset the parser. Note that you can only call this method immediately after you've created a parser; parser instances cannot be reused. Nzcannot reuse parsers)rrbs r"resetz Parser.reset s! y  "8     r*rMc|jrdS|j||_n|j|z|_|jr|jdkr^t t |j|j}|j|d|_|j|z |_|jdks|jsdS|j|j\}}|dkr.d|_d|_|dkrd|_t|ddS|j|d|_dS|jrdS tj |j5}tj |}dddn #1swxYwYt|dpt|d}|st |jdkr||jd\}}} } g|_tj|j|| |j|_|j|j|| |_|jt |jkr |j|jd|_d|_||_dS#t*$rYdSwxYw)z (Consumer) Feed data to the parser. :param data: A string buffer. :exception OSError: If the parser failed to parse the image file. NrrFrrr)finishedrrr0minrrrr#rrr rIrrDrrrXrFrrr) r\rskiprrr8rflagdoas r"feedz Parser.feeds =  F 9 DII D(DI <5 {Q3ty>>4;77 Idee, "kD0 ;??$)?F<&&ty11DAq1uu  ! q55!%DJ&q%8888F !"" DIII Z  D Z **(bBB((((((((((((((( r;//K72{3K3K (BG  1 1OO%%%!#JAq!Q BG#(#4RWaBDT#U#UDLL))"%333#$DK{c$)nn44$(Idkmm$< &'  %    s6I 'E< I E  I E I IIc|Srar(rbs r"rezParser.__enter__\rfr*r1rlc.|dSra)r[rns r"rozParser.__exit___s r*rc|jr;|ddx|_|_|jsd}t ||jsd}t ||jrt j|j5} tj ||_|j n#|j wxYw dddn #1swxYwY|jS)a (Consumer) Close the stream. :returns: An image object. :exception OSError: If the parser failed to parse the image file either because it cannot be identified or cannot be decoded. r*Nzimage was incompletezcannot parse this image) rrrr rrrrr rIr)r\r!r8s r"r[z Parser.closebs < # IIcNNN'+ +DI = #,cll"z +C#,,  9 &DI&& &"&!&BDJJOO%%%%DJOO%%%%%  & & & & & & & & & & & & & & & zs*;C=B0C0C  CC"Cr)rrMrr<)rrr)rr)r2r3r4r incrementalrr5rrr0r rrreror[r(r*r"rrs K $E$$$$D<@G@@@@ FH9999G G G G Rr*rrrr8 IO[bytes]rD list[_Tile]bufsizer<c |t|dsd|_|tt t ||jddz} |}| t|||||n7#ttj f$r}t||||d|Yd}~nd}~wwxYwt|dr| dSdS)zHelper to save image based on tile list :param im: Image object. :param fp: File object. :param tile: Tile list. :param bufsize: Optional buffer size encoderconfigr(rrNflush)rrrrr)maxrGrYrr _encode_tilerrUnsupportedOperation)rr8rDrfhexcs r"_saver!sGGIII 2 ' 'II)I (GRWQZ!^44G7 YY[[  RT7B//// B3 4777RT7D#666666667r7  s,;B((C>CCr int | Noner BaseException | Nonecv|D]4\}}}} |dkr||tj|j|| |j} | |j|| jr0| || d} nW|r; | |dd\} } | | | rn9n|J| ||} | dkrt| d| | #| wxYwdS)NrrTr)rr _getencoderrXrrr pushes_fdrencode_to_pyfdencodewriteencode_to_filer#r) rr8rDrrr  encoder_namer/r0r1rerrcoders r"rrsj04+ gvt A:: GGFOOO#BG\4AQRR    RUG , , ,  B b!!!!002215 B"(/w(?(?(C ""! "">>>%44RAAG{{"7D999sB OO    GOO    1s CD  D6rYrMc|dkrdS|tkr;||}t||krd}t||Sg}|}|dkrX|t |t}|sn-|||t|z}|dkXt d|D|krd}t|d|S)a Reads large blocks in a safe way. Unlike fp.read(n), this function doesn't trust the user. If the requested size is larger than SAFEBLOCK, the file is read block by block. :param fp: File handle. Must implement a read method. :param size: Number of bytes to read. :returns: A string containing size bytes of data. Raises an OSError if the file is truncated and the read cannot be completed rr*zTruncated File Readc34K|]}t|VdSra)r)rxblocks r" z_safe_read..s( * *%3u:: * * * * * *r*) SAFEBLOCKrrrr rsumjoin)r8rYrr!blocksremaining_sizer/s r" _safe_readr6s qyys ywwt}} t99t  'C#,,  FN 1  NI6677   e#e**$ 1    * *6 * * ***T11#cll 88F  r*ceZdZddZddZdS) PyCodecStaterr<c>d|_d|_d|_d|_dS)Nr)xsizeysizexoffyoffrbs r"rAzPyCodecState.__init__s"    r*tuple[int, int, int, int]cV|j|j|j|jz|j|jzfSra)r<r=r:r;rbs r"r/zPyCodecState.extentss'y$)TY%;TY=SSSr*Nr)rr>)r2r3r4rAr/r(r*r"r8r8sB TTTTTTr*r8cFeZdZUded<dd Zdd Zdd ZddZ dddZdS)PyCodeczIO[bytes] | NonefdrXr,r1rrr<cd|_t|_d|_||_||dSra)rr8rrBrXinit)r\rXr1s r"rAzPyCodec.__init__s515!^^   $r*tuple[Any, ...]c||_dS)z Override to perform codec specific initialization :param args: Tuple of arg items from the tile entry :returns: None N)r1rns r"rDz PyCodec.inits r*cdS)zT Override to perform codec specific cleanup :returns: None Nr(rbs r"rzPyCodec.cleanups r*rc||_dS)z Called from ImageFile to set the Python file-like object :param fd: A Python file-like object :returns: None N)rB)r\rBs r"rz PyCodec.setfd sr*NrImage.core.ImagingCorer/r.c||_|r|\}}}}|dks2|dks,||jjdks||jjdkrd}t|||j_||j_||z |j_||z |j_n#|jj\|j_|j_|jjdks|jjdkrd}t|dS)z Called from ImageFile to set the core output image for the codec :param im: A core image object :param extents: a 4 tuple of (x0, y0, x1, y1) defining the rectangle for this tile :returns: None rrz Tile cannot extend outside imagezSize must be positiveN)rrYrjrr<r=r:r;)r\rr/x0y0x1y1r!s r"rzPyCodec.setimages  >$NBBAvva2 Q#7#72 Q;O;O8 oo% DJO DJO!BwDJ !BwDJ  15 .DJ dj. : q DJ$4$9$9)CS// !%:$9r*)rXr,r1rrr<)r1rErr<r)rBrrr<ra)rrIr/r.rr<) r2r3r4r5rArDrrrr(r*r"rArAs    59!"!"!"!"!"!"!"r*rAcDeZdZdZdZeddZdd Z dddZd S) PyDecoderz Python implementation of a format decoder. Override this class and add the decoding logic in the :meth:`decode` method. See :ref:`Writing Your Own File Codec in Python` Frrc|jSra) _pulls_fdrbs r"rzPyDecoder.pulls_fdCs ~r*bufferImage.DecoderInputtuple[int, int]c$d}t|)a= Override to perform the decoding process. :param buffer: A bytes object with the data to be decoded. :returns: A tuple of ``(bytes consumed, errcode)``. If finished with decoding return -1 for the bytes consumed. Err codes are from :data:`.ImageFile.ERRORS`. zunavailable in base decoderNotImplementedError)r\rSr!s r"rzPyDecoder.decodeG,!#&&&r*Nr(rbytes | bytearrayrawmoderextrarEr<cd|s|j}tj|jd||}|jJ||j|j||}|ddkrd}t||ddkrd}t|dS)aS Convenience method to set the internal image from a stream of raw data :param data: Bytes to be set :param rawmode: The rawmode to be used for the decoder. If not specified, it will default to the mode of the image :param extra: Extra arguments for the decoder. :returns: None rNrznot enough image datarzcannot decode image data) rXr rrrrr/rrj)r\rr[r\r rr!s r" set_as_rawzPyDecoder.set_as_rawSs  iG  di ? ?w""" 47DJ..00111 HHTNN Q4199)CS// ! Q4199,CS// ! 9r*rr)rSrTrrU)Nr()rrZr[rr\rErr<) r2r3r4rrRpropertyrrr^r(r*r"rPrP9sxI X ' ' ' '#!# """""""r*rPcFeZdZdZdZeddZdd Zdd Zdd Z dS) PyEncoderz Python implementation of a format encoder. Override this class and add the decoding logic in the :meth:`encode` method. See :ref:`Writing Your Own File Codec in Python` Frrc|jSra) _pushes_fdrbs r"r&zPyEncoder.pushes_fd|s r*rrtuple[int, int, bytes]c$d}t|)a  Override to perform the encoding process. :param bufsize: Buffer size. :returns: A tuple of ``(bytes encoded, errcode, bytes)``. If finished with encoding return 1 for the error code. Err codes are from :data:`.ImageFile.ERRORS`. zunavailable in base encoderrW)r\rr!s r"r(zPyEncoder.encoderYr*rUc|jsdS|d\}}}|r#|jJ|j|||fS)z If ``pushes_fd`` is ``True``, then this method will be used, and ``encode()`` will only be called once. :returns: A tuple of ``(bytes consumed, errcode)``. Err codes are from :data:`.ImageFile.ERRORS`. )rrr)r&r(rBr))r\bytes_consumedr,rs r"r'zPyEncoder.encode_to_pyfds^~ 5(, A%  7&&& GMM$   w&&r*rcd}|dkrB||\}}}|dkrtj|||d|dkB|S)z :param fh: File handle. :param bufsize: Buffer size. :returns: If finished successfully, return 0. Otherwise, return an error code. Err codes are from :data:`.ImageFile.ERRORS`. rN)r(rJr))r\rrr,statusbufs r"r*zPyEncoder.encode_to_files_ll#';;w#7#7 FGSzzS\***llr*Nr_)rrrre)rrU)rrrrrr) r2r3r4rrdr`r&r(r'r*r(r*r"rbrbrsJ X ' ' ' ''''' r*rb)rrrrrr)r$r%rr)r) rrr8rrDrrrrr<ra)rrr8rrDrrrrr"r r#rr<)r8rrYrrrM)- __future__rrrrloggingrJrVtypingrrrrr?r r _utilr r TYPE_CHECKING_typingr getLoggerr2rrrGr1rrr#r)r%r7ABCrABCMetarPrr!rr6r8rArPrbr(r*r"rus8:#"""""  ,,,,,,,,,,,,)))))))) (''''''  8 $ $    N %    .....J...b$b$b$b$b$ b$b$b$J      #'        I    <@@@@@@@@LD!%     F    FTTTTTTTTE"E"E"E"E"E"E"E"P6"6"6"6"6"6"6"6"r8888888888r*