o
    ë6Wji(  ã                   @  sÐ   d dl mZ d dlZddlmZmZ dZer(d dlmZ d dl	m
Z
 d dlmZ G d	d
„ d
ƒZd(dd„Zd(dd„Zd)dd„Zd)dd„Zd)dd„Zd)dd„Zd*dd„ZeeeeeeedœZd+d$d%„Zd,d&d'„ZdS )-é    )ÚannotationsNé   )ÚImageÚ_imagingmathF)ÚCallable)ÚCodeType)ÚAnyc                   @  sj  e Zd ZdZdTdd„ZdUd	d
„Z		dVdWdd„ZdXdd„ZdYdd„ZdYdd„Z	dYdd„Z
dZdd„ZdZd d!„ZdZd"d#„ZdZd$d%„ZdZd&d'„ZdZd(d)„ZdZd*d+„ZdZd,d-„ZdZd.d/„ZdZd0d1„ZdZd2d3„ZdZd4d5„ZdYd6d7„ZdZd8d9„ZdZd:d;„ZdZd<d=„ZdZd>d?„ZdZd@dA„ZdZdBdC„ZdZdDdE„ZdZdFdG„ZdZdHdI„Z dZdJdK„Z!dZdLdM„Z"dZdNdO„Z#dZdPdQ„Z$dZdRdS„Z%dS )[Ú_Operandz4Wraps an image operand, providing standard operatorsÚimúImage.Imagec                 C  s
   || _ d S ©N)r
   )Úselfr
   © r   ú\/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/PIL/ImageMath.pyÚ__init__!   s   
z_Operand.__init__Úim1ú_Operand | floatÚreturnc                 C  sˆ   t |tƒr%|jjdv r|j d¡S |jjdv r|jS d|jj› �}t|ƒ‚t |ttfƒr;| jjdv r;t 	d| jj
|¡S t 	d| jj
|¡S )N)Ú1ÚLÚI)r   ÚFzunsupported mode: )r   r   r   r   )Ú
isinstancer	   r
   ÚmodeÚconvertÚ
ValueErrorÚintÚfloatr   ÚnewÚsize)r   r   Úmsgr   r   r   Ú__fixup$   s   
z_Operand.__fixupNÚopÚstrÚim2ú_Operand | float | Noner   ú
str | Nonec              
   C  sº  |   |¡}|d u rHt |p|j|jd ¡}ztt|› d|j› �ƒ}W n ty8 } zd|› d�}t|ƒ|‚d }~ww t 	|| 
¡ | 
¡ ¡ t|ƒS |   |¡}	|j|	jkrg|jdkr]| d¡}|	jdkrg|	 d¡}	|j|	jkr›t|jd |	jd ƒt|jd |	jd ƒf}
|j|
kr�| d|
 ¡}|	j|
kr›|	 d|
 ¡}	t |p¡|j|jd ¡}ztt|› d|j› �ƒ}W n tyÊ } zd|› d�}t|ƒ|‚d }~ww t || 
¡ | 
¡ |	 
¡ ¡ t|ƒS )NÚ_zbad operand type for 'ú'r   r   r   )r   r   )Ú_Operand__fixupr   r   r   r   Úgetattrr   ÚAttributeErrorÚ	TypeErrorÚunopÚgetimr   ÚminÚcropÚbinopr	   )r   r"   r   r$   r   Úim_1ÚoutÚer    Úim_2r   r   r   r   Úapply6   sH   

€þ
è



þ


€þz_Operand.applyÚboolc                 C  s   | j  ¡ d uS r   )r
   Úgetbbox©r   r   r   r   Ú__bool__d   s   z_Operand.__bool__c                 C  ó   |   d| ¡S )NÚabs©r6   r9   r   r   r   Ú__abs__h   ó   z_Operand.__abs__c                 C  s   | S r   r   r9   r   r   r   Ú__pos__k   s   z_Operand.__pos__c                 C  r;   )NÚnegr=   r9   r   r   r   Ú__neg__n   r?   z_Operand.__neg__Úotherc                 C  ó   |   d| |¡S ©NÚaddr=   ©r   rC   r   r   r   Ú__add__r   ó   z_Operand.__add__c                 C  ó   |   d|| ¡S rE   r=   rG   r   r   r   Ú__radd__u   rI   z_Operand.__radd__c                 C  rD   ©NÚsubr=   rG   r   r   r   Ú__sub__x   rI   z_Operand.__sub__c                 C  rJ   rL   r=   rG   r   r   r   Ú__rsub__{   rI   z_Operand.__rsub__c                 C  rD   ©NÚmulr=   rG   r   r   r   Ú__mul__~   rI   z_Operand.__mul__c                 C  rJ   rP   r=   rG   r   r   r   Ú__rmul__�   rI   z_Operand.__rmul__c                 C  rD   ©NÚdivr=   rG   r   r   r   Ú__truediv__„   rI   z_Operand.__truediv__c                 C  rJ   rT   r=   rG   r   r   r   Ú__rtruediv__‡   rI   z_Operand.__rtruediv__c                 C  rD   ©NÚmodr=   rG   r   r   r   Ú__mod__Š   rI   z_Operand.__mod__c                 C  rJ   rX   r=   rG   r   r   r   Ú__rmod__�   rI   z_Operand.__rmod__c                 C  rD   ©NÚpowr=   rG   r   r   r   Ú__pow__�   rI   z_Operand.__pow__c                 C  rJ   r\   r=   rG   r   r   r   Ú__rpow__“   rI   z_Operand.__rpow__c                 C  r;   )NÚinvertr=   r9   r   r   r   Ú
__invert__—   r?   z_Operand.__invert__c                 C  rD   ©NÚandr=   rG   r   r   r   Ú__and__š   rI   z_Operand.__and__c                 C  rJ   rb   r=   rG   r   r   r   Ú__rand__�   rI   z_Operand.__rand__c                 C  rD   ©NÚorr=   rG   r   r   r   Ú__or__    rI   z_Operand.__or__c                 C  rJ   rf   r=   rG   r   r   r   Ú__ror__£   rI   z_Operand.__ror__c                 C  rD   ©NÚxorr=   rG   r   r   r   Ú__xor__¦   rI   z_Operand.__xor__c                 C  rJ   rj   r=   rG   r   r   r   Ú__rxor__©   rI   z_Operand.__rxor__c                 C  rD   )NÚlshiftr=   rG   r   r   r   Ú
__lshift__¬   rI   z_Operand.__lshift__c                 C  rD   )NÚrshiftr=   rG   r   r   r   Ú
__rshift__¯   rI   z_Operand.__rshift__c                 C  rD   )NÚeqr=   rG   r   r   r   Ú__eq__³   rI   z_Operand.__eq__c                 C  rD   )NÚner=   rG   r   r   r   Ú__ne__¶   rI   z_Operand.__ne__c                 C  rD   )NÚltr=   rG   r   r   r   Ú__lt__¹   rI   z_Operand.__lt__c                 C  rD   )NÚler=   rG   r   r   r   Ú__le__¼   rI   z_Operand.__le__c                 C  rD   )NÚgtr=   rG   r   r   r   Ú__gt__¿   rI   z_Operand.__gt__c                 C  rD   )NÚger=   rG   r   r   r   Ú__ge__Â   rI   z_Operand.__ge__)r
   r   )r   r   r   r   )NN)
r"   r#   r   r   r$   r%   r   r&   r   r	   )r   r7   )r   r	   )rC   r   r   r	   )&Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   r6   r:   r>   r@   rB   rH   rK   rN   rO   rR   rS   rV   rW   rZ   r[   r^   r_   ra   rd   re   rh   ri   rl   rm   ro   rq   rs   ru   rw   ry   r{   r}   r   r   r   r   r	      sL    

û
.




























r	   r   r   c                 C  ó   t | j d¡ƒS )Nr   ©r	   r
   r   r9   r   r   r   Úimagemath_intÇ   ó   r„   c                 C  r‚   )Nr   rƒ   r9   r   r   r   Úimagemath_floatË   r…   r†   rC   r%   c                 C  ó   | j d| |dd�S )Nrr   r   ©r   r=   rG   r   r   r   Úimagemath_equalÐ   ó   r‰   c                 C  r‡   )Nrt   r   rˆ   r=   rG   r   r   r   Úimagemath_notequalÔ   rŠ   r‹   c                 C  rD   )Nr/   r=   rG   r   r   r   Úimagemath_minØ   rI   rŒ   c                 C  rD   )NÚmaxr=   rG   r   r   r   Úimagemath_maxÜ   rI   rŽ   r   r#   c                 C  s   t | j |¡ƒS r   rƒ   )r   r   r   r   r   Úimagemath_convertà   r…   r�   )r   r   ÚequalÚnotequalr/   r�   r   Ú
expressionúCallable[[dict[str, Any]], Any]Úkwr   c                 K  sd   t  ¡ }| |¡ | ¡ D ]\}}t|tjƒrt|ƒ||< q| |ƒ}z|jW S  ty1   | Y S w )a1  
    Returns the result of an image function.

    :py:mod:`~PIL.ImageMath` only supports single-layer images. To process multi-band
    images, use the :py:meth:`~PIL.Image.Image.split` method or
    :py:func:`~PIL.Image.merge` function.

    :param expression: A function that receives a dictionary.
    :param **kw: Values to add to the function's dictionary.
    :return: The expression result. This is usually an image object, but can
             also be an integer, a floating point value, or a pixel tuple,
             depending on the expression.
    )	ÚopsÚcopyÚupdateÚitemsr   r   r	   r
   r+   )r’   r”   ÚargsÚkÚvr3   r   r   r   Úlambda_evalï   s   
€ÿrœ   c                   sÀ   t  ¡ ‰ |D ]}d|v stt|ƒrd|› d�}t|ƒ‚qˆ  |¡ ˆ  ¡ D ]\}}t|tjƒr5t	|ƒˆ |< q%t
| ddƒ‰d‡ ‡‡fd
d„‰ˆˆƒ t | dˆ ¡}z|jW S  ty_   | Y S w )a-  
    Evaluates an image expression. This uses Python's ``eval()`` function to process
    the expression string, and carries the security risks of doing so. It is not
    recommended to process expressions without considering this.
    :py:meth:`~lambda_eval` is a more secure alternative.

    :py:mod:`~PIL.ImageMath` only supports single-layer images. To process multi-band
    images, use the :py:meth:`~PIL.Image.Image.split` method or
    :py:func:`~PIL.Image.merge` function.

    :param expression: A string containing a Python-style expression.
    :param **kw: Values to add to the evaluation context.
    :return: The evaluated expression. This is usually an image object, but can
             also be an integer, a floating point value, or a pixel tuple,
             depending on the expression.
    Ú__r(   ú' not allowedz<string>ÚevalÚcoder   r   ÚNonec                   sX   | j D ]}t|ƒtˆƒu rˆ|ƒ q| jD ]}|ˆ vr)|dkr)d|› d�}t|ƒ‚qd S )Nr<   r(   rž   )Ú	co_constsÚtypeÚco_namesr   )r    ÚconstÚnamer    ©r™   Úcompiled_codeÚscanr   r   r©   +  s   
€
€ýzunsafe_eval.<locals>.scanN)r    r   r   r¡   )r•   r–   ÚhasattrÚbuiltinsr   r—   r˜   r   r   r	   ÚcompilerŸ   r
   r+   )r’   r”   rš   r    r›   r3   r   r§   r   Úunsafe_eval  s(   þ
€
ÿr­   )r   r	   r   r	   )r   r	   rC   r%   r   r	   )r   r	   r   r#   r   r	   )r’   r“   r”   r   r   r   )r’   r#   r”   r   r   r   )Ú
__future__r   r«   Ú r   r   ÚTYPE_CHECKINGÚcollections.abcr   Útypesr   Útypingr   r	   r„   r†   r‰   r‹   rŒ   rŽ   r�   r•   rœ   r­   r   r   r   r   Ú<module>   s6    
*





ù
