CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
ImageFile.cpython-310.pyc280 linesDownload Raw Back to __pycache__
1o

2��Yi�x�@s�ddlmZddlZddlZddlZddlZddlZddlZddlm	Z	m3Z4mZmZddl
mZmZddlmZmZdZerDddlmZe�e�Zd	Z	d5ZdZ	ddd
ddd�Z	dCdd�ZdDdd�ZGdd�de�ZGdd�dej�Z Gd d!�d!ej!�Z"Gd"d#�d#e ej#d$�Z$Gd%d&�d&�Z%dEdFd/d0�Z&	dGdHd5d6�Z'dId9d:�Z(Gd;d<�d<�Z)Gd=d>�d>�Z*Gd?d@�d@e*�Z+GdAdB�dBe*�Z,dS)J�)�annotationsN)�IO�Any�6NamedTuple�cast�)�ExifTags�Image)�
DeferredError�is_pathF)�StrOrBytesPathiizimage buffer overrun errorzdecoding errorz
unknown errorzbad configurationzout of memory error)��������������������i�����error�int�encoder�bool�return�OSErrorcCshztj�|�}Wntyt�|�}Ynw|s$|rdnd�d|��}|d|r*dnd�d�7}t|�S)Nr�decoderz error z when �writing�readingz image file)r	�coreZgetcodecstatus�AttributeError�ERRORS�getr)rr�msg�r�wE:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\PIL/ImageFile.py�_get_oserrorVs�r!�t�_TilecCs|dS)N�r)r"rrr �	_tilesortasr%c@s6eZdZUded<ded<dZded<dZd	ed7<dS)r#�strZ8codec_name� tuple[int, int, int, int] | None�extentsrr�offsetNztuple[Any, ...] | str | None�args)�__name__�9__module__�__qualname__�__annotations__r)r*rrrr r#fs1011cs�eZdZdZ	d1d2�fd	d12�
Zd3dd�Zd4d
d�Zd3dd�Zd5dd�Zd3�fdd�Z	d6dd�Z13d7dd�Zd8�fdd�Zd9�fd!d"�Z
d3d#d$�Zd:d&d'�Zd3d(d)�Zd3d*d+�Zd;d/d0�Z�ZS)<�	ImageFilez*Base class for image file format handlers.N�fp�StrOrBytesPath | IO[bytes]�filename�str | bytes | Noner�Nonecst���d|_d|_g|_	d|_d|_t|_||t	|�r0t14|d�|_t�
|�|_d|_nttt|�|_|dur>|nd|_d|_z8z|��Wntttttjfyb}zt|�|�d}~ww|jrt|jddkst|jddkrzd}t|��WdSty�|jr�|j���w)	Nrrr�rbT�Fznot identified by this driver)�super�__init__�15_min_frame�custom_mimetype�tile�readonly�
decoderconfig�MAXBLOCK�decodermaxblockr�openr0�os�fspathr2�
_exclusive_fprr�bytes�_open�16IndexError�	TypeError�KeyError�EOFError�structr�SyntaxError�mode�size�
BaseException�close)�selfr0r2�vr��	__class__rr r8usN17�18��"	�19�zImageFile.__init__cC�dS�Nr�rPrrr rE��zImageFile._opencC�|SrUrrVrrr �	__enter__�rWzImageFile.__enter__cCsTt|dd�rt|jt�s|j|jkr|j��ttd��|_|jr(|j��dSdS)N�_fpFzOperation on closed image)�getattr�20isinstancerZr21r0rO�22ValueErrorrVrrr �	_close_fp�s23�zImageFile._close_fpr*�objectcGst|dd�r24|��d|_dS)NrCF)r[r^r0�rPr*rrr �__exit__�s25zImageFile.__exit__c26sRz	|��d|_Wnty!}zt�d|�WYd}~nd}~wwt���dS)a�27        Closes the file pointer, if possible.28 29        This operation will destroy the image core and release its memory.30        The image data will be unusable afterward.31 32        This function is required to close images that have multiple frames or33        have not had their file read and closed by the34        :py:meth:`~PIL.Image.Image.load` method. See :ref:`file-handling` for35        more information.36        NzError closing: %s)r^r0�	Exception�logger�debugr7rO)rPrrRrr rO�s37��zImageFile.close�list[ImageFile]c	Cs�g}|��}g}tjj|vr-|tjj}|r-t|t�s|f}|D]}|�|�|�|f�q |�tj	j38�}|rM|�tjj�rM|j
dusDJ�|�||j
jf�d}|D]�\}}	|jdus\J�|j��}39|durg|40}|j}|dur�|�tjj�}|dur�|t|dd�7}|j�|�|�tjj�}
t|
t�s�J�|j�|
�}t�|�}t�|��*}ddlm}|dur�t||j�r�|	g|_|�d�|��|�|�Wd�n1s�wYqQ|dur�|jdus�J�|j�|�|S)NZ_exif_offsetrr)�TiffImagePlugin) ZgetexifrZBaseZSubIFDsr\�tuple�appendZ
_get_ifd_dictZget_ifdZIFDZIFD1rZJpegIFOffset�_info�nextr0�tellr[�seekZJpegIFByteCountr�read�io�BytesIOr	r@r6rfZ
TiffImageFileZ41_frame_posZ_seek�load)rPZchild_imagesZexifZifdsZsubifd_offsetsZ
subifd_offsetZifd1r)ZifdZ42ifd_offset�current_offsetr0Zthumbnail_offset�length�data�imrfrrr �get_child_images�sZ43444546�47��zImageFile.get_child_images�48str | NonecCs,|jr|jS|jdurtj�|j���SdSrU)r:�formatr	�MIMEr�upperrVrrr �get_format_mimetypes4950zImageFile.get_format_mimetype�	list[Any]cst���|jgSrU)r7�__getstate__r2rVrRrr r|szImageFile.__getstate__�statecs,g|_t|�dkr|d|_t��|�dS)N�)r;�lenr2r7�__setstate__)rPr}rRrr r�s51zImageFile.__setstate__cCs |jr|jr|j��d|_dS)zCheck file integrityN)rCr0rOrVrrr �verifys5253zImageFile.verify�Image.core.PixelAccess | NonecCs�|js|jdurd}t|��tj�|�}|js|Sd|_|jo%t|j�dk}|jdus-J�d}t	|d�r:|j54}d}n|jj}t	|d�rI|j}d}n|jj
}|r�|jd\}}}	}55t|56t�rb|57ddf}58|dkr�t|59t�r�t|60�d	kr�|61d|jkr�|62dtjvr�|	dkr�d63}t|��zSddl}t|j��}|j|��d|jd�|_Wd�n1s�wY|	|jd|64d|j��kr�d}t|��tj�|j|j||	|65�|_d}|jr�d|j_Wntttfy�d|_Ynw|� �d
}
|j�s�|jj!t"d�t#|dd�}dd�t$�%|jdd��D�|_t&|j�D]�\}\}}}	}66||	�t�'|j||67|j(�}z�|�)|j|�|j*�rG|�+|j�|�,d�d}
nt|}	|j-}|dt|j�k�rh|j|dj.}||	k�rh||	}z||�}Wn t/t0j1f�y�}zt2�r�WYd}~n8d}t|�|�d}~ww|�s�t2�r�n%dt|��d�}t|��||}|�,|�\}}
|dk�r�n||d�}�qJW|�3��q|�3�wg|_||_4|�5�|j6�r�|j7�r�|j�8�d|_|j�s�t2�s�|
dk�r�t9|
dd��tj�|�S)z"Load image data based on tile listNzcannot load this imagerr�	load_readF�	load_seek�raw�zTile offset cannot be negative)�accesszbuffer is not large enoughr��keyZtile_prefix�cSsg|]68\}}t|�d�qS)r
)�list)�.0�_Ztilesrrr �69<listcomp>is70��z"ImageFile.load.<locals>.<listcomp>cSs|d|d|dfS)Nrrr�r)r;rrr �<lambda>lsz ImageFile.load.<locals>.<lambda>Tzimage file is truncatedzimage file is truncated (z bytes not processed)�r):r;�_imrr	rp�mapr2rr0�hasattrr�rmr�rlr\r&rgrLZ	_MAPMODESr]�mmapr@�fileno�ACCESS_READrMrZ71map_bufferrt�paletteZdirtyr�ImportError�load_prepare�sortr%r[�	itertools�groupby�	enumerate�_getdecoderr=�setimage�pulls_fd�setfd�decoder?r)rFrJr�LOAD_TRUNCATED_IMAGES�cleanupr<�load_endrCZ!_close_exclusive_fp_after_loadingrOr!)rPrZpixelZuse_mmapr<rmrlZdecoder_namer(r)r*r�r0Zerr_code�prefix�ir�b�72read_bytes�next_offset�s�e�nrrr rps�73747576�� ��77�78���7980����81��!82zImageFile.loadcCs<|jdurtj�|j|j�|_|jdkrtj�|�dSdS)N�P)r�r	r�newrLrMrtrprVrrr r��s838485�zImageFile.load_preparecCrTrUrrVrrr r���zImageFile.load_end�framerrcCsJ||jkst|d�r|jdus|t|d�|jkrd}t|��|��|kS)N�	_n_framesZn_framesz attempt to seek outside sequence)r9r�r�r[rIrk)rPr�rrrr �_seek_check�s86�87zImageFile._seek_checkrU)r0r1r2r3rr4�rr4)rr/�r*r_rr4)rre)rrv)rr{)r}r{rr4�rr�)r�rrr)r+r,r-�__doc__r8rErYr^rarOrurzr|r�r�rpr�r�r��
__classcell__rrrRr r/rs&�884899091929339495	9697r/c@s&eZdZddd�Zejddd	��Zd98S)
�StubHandlerrt�
StubImageFilerr4cCrTrUr�rPrtrrr r@�rWzStubHandler.open�Image.ImagecCrTrUrr�rrr rp�r�zStubHandler.loadN)rtr�rr4)rtr�rr�)r+r,r-r@�abc�abstractmethodrprrrr r��s99r�c@s:eZdZdZejd
dd��Zddd�Zejdd100d��ZdS)r�z�101    Base class for stub image loaders.102 103    A stub loader is an image loader that can identify files of a104    certain format, but relies on external code to load the file.105    rr4cCrTrUrrVrrr rE�r�zStubImageFile._openr�cCsT|��}|durd|j�d�}t|��|�|�}|dusJ�|j|_|j|_|��S)Nzcannot find loader for this z file)�_loadrwrrprS�__dict__)rP�loaderr�imagerrr rp�s106zStubImageFile.load�StubHandler | NonecC�dS)z (Hook) Find actual image loader.NrrVrrr r��szStubImageFile._loadNr�r�)rr�)	r+r,r-r�r�r�rErpr�rrrr r��s107r�)�	metaclassc@steZdZUdZdZdZded<dZded<dZded<d	Z	d	Z108ddd
�Zddd�Zddd�Z
ddd�Zddd�ZdS)�Parserzj109    Incremental image parser.  This class implements the standard110    feed/close consumer interface.111    NzImage.Image | Noner�zbytes | Nonersz,Image.core.ImagingDecoder | PyDecoder | Nonerrrr4cCs|jdus	Jd��dS)z�112        (Consumer) Reset the parser.  Note that you can only call this113        method immediately after you've created a parser; parser114        instances cannot be reused.115        Nzcannot reuse parsers)rsrVrrr �resetszParser.resetrDcCs�|jrdS|jdur||_n|j||_|jri|jdkr=tt|j�|j�}|j|d�|_|j||_|jdks;|js=dS|j�|j�\}}|dkr_d|_d|_|dkr]d|_t|dd��dS|j|d�|_dS|jrndSzt	�116|j��
}t�|�}Wd�n1s�wYWn117t
y�YdSwt|d�p�t|d�}|s�t|j�dkr�d|_n:|��|jd\}}}	}118g|_t�|j||119|j�|_|j�|j|�|	|_|jt|j�kr�|j|jd�|_d|_||_dS)z�120        (Consumer) Feed data to the parser.121 122        :param data: A string buffer.123        :exception OSError: If the parser failed to parse the image file.124        NrrFr�r�r�)�finishedrsrr)�minrr�r�r!rnror	r@rr�r;r�r�rLr=r�rt)rPrs�skipr�r�r0rt�flag�d�o�arrr �feed
sX	125126���127zParser.feedcCrXrUrrVrrr rYYrWzParser.__enter__r*r_cGs|��dSrU)rOr`rrr ra\szParser.__exit__r�c	Cs�|jr|�d�d|_|_|jsd}t|��|js d}t|��|jrOt�|j��}z
t�	|�|_W|j�128�n|j�129�wWd�|jS1sJwY|jS)a130        (Consumer) Close the stream.131 132        :returns: An image object.133        :exception OSError: If the parser failed to parse the image file either134                            because it cannot be identified or cannot be135                            decoded.136        r�Nzimage was incompletezcannot parse this image)rr�rsr�rr�rnror	r@rp)rPrr0rrr rO_s$137 138139��zParser.closer�)rsrDrr4)rr�r�)rr�)r+r,r-r��incrementalr�r.rsrr)r�r�r�rYrarOrrrr r��s140141142143L144r�rtr�r0�	IO[bytes]r;�list[_Tile]�bufsizer4c145Cs�|��t|d�sd|_|jtd�tt||jdd�}z|��}|�	�t146|||||�Wnttj
fyM}zt147||||d|�WYd}~nd}~wwt|d�rY|�	�dSdS)z�Helper to save image based on tile list148 149    :param im: Image object.150    :param fp: File object.151    :param tile: Tile list.152    :param bufsize: Optional buffer size153    �
encoderconfigrr�r�N�flush)rpr�r�r�r%�maxr>rMr�r��_encode_tilerrn�UnsupportedOperation)rtr0r;r��fh�excrrr �_save�s 	154��155�r�r��156int | Noner��BaseException | Nonec
	Cs�|D]i\}}}}	|dkr|�|�t�|j||	|j�}157zK|158�|j|�|159jr2|160�|�|161�	�d}n$|rJ	|162163|�dd�\}}|�|�|rHnq5n|dusPJ�|164�||�}|dkrat
|dd�|�W|165��q|166��wdS)NrrTr�)rlr	Z_getencoderrLr�r�rt�	pushes_fdr��encode_to_pyfd�encode�write�encode_to_filer!r�)
rtr0r;r�r�r�Zencoder_namer(r)r*r�errcodersrrr r��s0167168169���r�rMrDcCs�|dkrdS|tkr|�|�}t|�|krd}t|��|Sg}|}|dkr?|�t|t��}|s0n|�|�|t|�8}|dks%tdd�|D��|krPd}t|��d�|�S)a�170    Reads large blocks in a safe way.  Unlike fp.read(n), this function171    doesn't trust the user.  If the requested size is larger than172    SAFEBLOCK, the file is read block by block.173 174    :param fp: File handle.  Must implement a <b>read</b> method.175    :param size: Number of bytes to read.176    :returns: A string containing <i>size</i> bytes of data.177 178    Raises an OSError if the file is truncated and the read cannot be completed179 180    rr�zTruncated File Readcss�|]}t|�VqdSrU)r)r��blockrrr �	<genexpr>�s�z_safe_read.<locals>.<genexpr>)�	SAFEBLOCKrmrrr�rh�sum�join)r0rMrsr�blocksZremaining_sizer�rrr �181_safe_read�s*
182183�184r�c@s eZdZd	dd�Zd185dd�ZdS)�PyCodecStaterr4cCsd|_d|_d|_d|_dS)Nr)�xsize�ysize�xoff�yoffrVrrr r8�s186zPyCodecState.__init__�tuple[int, int, int, int]cCs |j|j|j|j|j|jfSrU)r�r�r�r�rVrrr r(�s zPyCodecState.extentsNr�)rr�)r+r,r-r8r(rrrr r��s187r�c@sLeZdZUded<dd	d188�Zddd
�Zddd�Zddd�Z	dddd�ZdS) �PyCodeczIO[bytes] | None�fdrLr&r*rrr4cGs(d|_t�|_d|_||_|�|�dSrU)rtr�r}r�rL�init)rPrLr*rrr r8�s189zPyCodec.__init__�tuple[Any, ...]cC�190||_dS)z�191        Override to perform codec specific initialization192 193        :param args: Tuple of arg items from the tile entry194        :returns: None195        N)r*r`rrr r���196zPyCodec.initcCr�)zT197        Override to perform codec specific cleanup198 199        :returns: None200        NrrVrrr r�szPyCodec.cleanupr�cCr�)z�201        Called from ImageFile to set the Python file-like object202 203        :param fd: A Python file-like object204        :returns: None205        N)r�)rPr�rrr r�	r�z
PyCodec.setfdNrt�Image.core.ImagingCorer(r'cCs�||_|r|\}}}}nd\}}}}|dkr%|dkr%|jj\|j_|j_n||j_||j_|||j_|||j_|jjdksE|jjdkrKd}t|��|jj|jj|jjdksg|jj|jj|jjdkrmd}t|��dS)z�206        Called from ImageFile to set the core output image for the codec207 208        :param im: A core image object209        :param extents: a 4 tuple of (x0, y0, x1, y1) defining the rectangle210            for this tile211        :returns: None212        )rrrrrzSize cannot be negativerz Tile cannot extend outside imageN)rtrMr}r�r�r�r�r])rPrtr(Zx0Zy0�x1�y1rrrr r�s$�zPyCodec.setimage)rLr&r*rrr4)r*r�rr4r�)r�r�rr4rU)rtr�r(r'rr4)	r+r,r-r.r8r�r�r�r�rrrr r��s213214215216	217�r�c@s:eZdZdZdZeddd��Zdd218d�Z	
dddd�ZdS)�	PyDecoderz�219    Python implementation of a format decoder. Override this class and220    add the decoding logic in the :meth:`decode` method.221 222    See :ref:`Writing Your Own File Codec in Python<file-codecs-py>`223    FrrcC�|jSrU)�	_pulls_fdrVrrr r�F�zPyDecoder.pulls_fd�buffer�$bytes | Image.SupportsArrayInterface�tuple[int, int]cC�d}t|��)a=224        Override to perform the decoding process.225 226        :param buffer: A bytes object with the data to be decoded.227        :returns: A tuple of ``(bytes consumed, errcode)``.228            If finished with decoding return -1 for the bytes consumed.229            Err codes are from :data:`.ImageFile.ERRORS`.230        zunavailable in base decoder��NotImplementedError)rPr�rrrr r�J�	zPyDecoder.decodeNrrsrD�rawmoderv�extrar�r4cCs||s|j}t�|jd||�}|jdusJ�|�|j|j���|�|�}|ddkr0d}t|��|ddkr<d}t|��dS)aS231        Convenience method to set the internal image from a stream of raw data232 233        :param data: Bytes to be set234        :param rawmode: The rawmode to be used for the decoder.235            If not specified, it will default to the mode of the image236        :param extra: Extra arguments for the decoder.237        :returns: None238        r�Nrznot enough image datarzcannot decode image data)	rLr	r�rtr�r}r(r�r])rPrsrrr�r�rrrr �239set_as_rawVs
240�zPyDecoder.set_as_raw�rr)r�rrr)Nr)rsrDrrvrr�rr4)	r+r,r-r�r��propertyr�r�rrrrr r�<s241
�r�c@s@eZdZdZdZeddd��Zdd242d�Zdd
d�Zddd�Z	dS)�	PyEncoderz�243    Python implementation of a format encoder. Override this class and244    add the decoding logic in the :meth:`encode` method.245 246    See :ref:`Writing Your Own File Codec in Python<file-codecs-py>`247    FrrcCr�rU)�248_pushes_fdrVrrr r�|r�zPyEncoder.pushes_fdr�r�tuple[int, int, bytes]cCr)a 249        Override to perform the encoding process.250 251        :param bufsize: Buffer size.252        :returns: A tuple of ``(bytes encoded, errcode, bytes)``.253            If finished with encoding return 1 for the error code.254            Err codes are from :data:`.ImageFile.ERRORS`.255        zunavailable in base encoderr)rPr�rrrr r��rzPyEncoder.encodercCs@|jsdS|�d�\}}}|r|jdusJ�|j�|�||fS)z�256        If ``pushes_fd`` is ``True``, then this method will be used,257        and ``encode()`` will only be called once.258 259        :returns: A tuple of ``(bytes consumed, errcode)``.260            Err codes are from :data:`.ImageFile.ERRORS`.261        )rrrN)r�r�r�r�)rPZbytes_consumedr�rsrrr r��szPyEncoder.encode_to_pyfdr�cCsDd}|dkr |�|�\}}}|dkrt�|||d��|dks|S)z�262        :param fh: File handle.263        :param bufsize: Buffer size.264 265        :returns: If finished successfully, return 0.266            Otherwise, return an error code. Err codes are from267            :data:`.ImageFile.ERRORS`.268        rN)r�rAr�)rPr�r�r��status�bufrrr r��s	�zPyEncoder.encode_to_fileNr	)r�rrr
)rr)r�rr�rrr)269r+r,r-r�rr270r�r�r�r�rrrr rrs271272r)rrrrrr)r"r#rr)r)273rtr�r0r�r;r�r�rrr4rU)rtr�r0r�r;r�r�rr�r�r�r�rr4)r0r�rMrrrD)-�274__future__rr�rnr��loggingrArJ�typingrrrrr6rr	Z_utilr275r�
TYPE_CHECKINGZ_typingr�	getLoggerr+rcr>r�r�rr!r%r#r/�ABCr��ABCMetar�r�r�r�r�r�r�r�rrrrr �<module>sX276
�277278a	279"�280##N6
Aluode/PerceptionLabPortable · CoolFace