o
    sh.                     @  s   d dl mZ d dlZd dlmZ d dlmZmZ ddlm	Z	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i fd/d%d&Zi fd0d'd(Zi fd1d*d+ZdS )2    )annotationsN)CodeType)AnyCallable   )Image_imagingmath)	deprecatec                   @  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   a/var/www/html/construction_image-detection-poc/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rt|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   apply3   sH   









z_Operand.applyboolc                 C  s   | j  d uS r   )r   getbboxr   r   r   r   __bool__a   s   z_Operand.__bool__c                 C     |  d| S )Nabsr7   r:   r   r   r   __abs__e      z_Operand.__abs__c                 C  s   | S r   r   r:   r   r   r   __pos__h   s   z_Operand.__pos__c                 C  r<   )Nnegr>   r:   r   r   r   __neg__k   r@   z_Operand.__neg__otherc                 C     |  d| |S Naddr>   r   rD   r   r   r   __add__o      z_Operand.__add__c                 C     |  d|| S rF   r>   rH   r   r   r   __radd__r   rJ   z_Operand.__radd__c                 C  rE   Nsubr>   rH   r   r   r   __sub__u   rJ   z_Operand.__sub__c                 C  rK   rM   r>   rH   r   r   r   __rsub__x   rJ   z_Operand.__rsub__c                 C  rE   Nmulr>   rH   r   r   r   __mul__{   rJ   z_Operand.__mul__c                 C  rK   rQ   r>   rH   r   r   r   __rmul__~   rJ   z_Operand.__rmul__c                 C  rE   Ndivr>   rH   r   r   r   __truediv__   rJ   z_Operand.__truediv__c                 C  rK   rU   r>   rH   r   r   r   __rtruediv__   rJ   z_Operand.__rtruediv__c                 C  rE   Nmodr>   rH   r   r   r   __mod__   rJ   z_Operand.__mod__c                 C  rK   rY   r>   rH   r   r   r   __rmod__   rJ   z_Operand.__rmod__c                 C  rE   Npowr>   rH   r   r   r   __pow__   rJ   z_Operand.__pow__c                 C  rK   r]   r>   rH   r   r   r   __rpow__   rJ   z_Operand.__rpow__c                 C  r<   )Ninvertr>   r:   r   r   r   
__invert__   r@   z_Operand.__invert__c                 C  rE   Nandr>   rH   r   r   r   __and__   rJ   z_Operand.__and__c                 C  rK   rc   r>   rH   r   r   r   __rand__   rJ   z_Operand.__rand__c                 C  rE   Norr>   rH   r   r   r   __or__   rJ   z_Operand.__or__c                 C  rK   rg   r>   rH   r   r   r   __ror__   rJ   z_Operand.__ror__c                 C  rE   Nxorr>   rH   r   r   r   __xor__   rJ   z_Operand.__xor__c                 C  rK   rk   r>   rH   r   r   r   __rxor__   rJ   z_Operand.__rxor__c                 C  rE   )Nlshiftr>   rH   r   r   r   
__lshift__   rJ   z_Operand.__lshift__c                 C  rE   )Nrshiftr>   rH   r   r   r   
__rshift__   rJ   z_Operand.__rshift__c                 C  rE   )Neqr>   rH   r   r   r   __eq__   rJ   z_Operand.__eq__c                 C  rE   )Nner>   rH   r   r   r   __ne__   rJ   z_Operand.__ne__c                 C  rE   )Nltr>   rH   r   r   r   __lt__   rJ   z_Operand.__lt__c                 C  rE   )Nler>   rH   r   r   r   __le__   rJ   z_Operand.__le__c                 C  rE   )Ngtr>   rH   r   r   r   __gt__   rJ   z_Operand.__gt__c                 C  rE   )Nger>   rH   r   r   r   __ge__   rJ   z_Operand.__ge__)r   r   )r   r   r   r   )NN)
r#   r$   r   r   r%   r&   r   r'   r   r
   )r   r8   )r   r
   )rD   r   r   r
   )&__name__
__module____qualname____doc__r   r*   r7   r;   r?   rA   rC   rI   rL   rO   rP   rS   rT   rW   rX   r[   r\   r_   r`   rb   re   rf   ri   rj   rm   rn   rp   rr   rt   rv   rx   rz   r|   r~   r   r   r   r   r
      sL    


.




























r
   r   r   c                 C     t | jdS )Nr   r
   r   r   r:   r   r   r   imagemath_int      r   c                 C  r   )Nr   r   r:   r   r   r   imagemath_float   r   r   rD   r&   c                 C     | j d| |ddS )Nrs   r   r   r>   rH   r   r   r   imagemath_equal      r   c                 C  r   )Nru   r   r   r>   rH   r   r   r   imagemath_notequal   r   r   c                 C  rE   )Nr0   r>   rH   r   r   r   imagemath_min   rJ   r   c                 C  rE   )Nmaxr>   rH   r   r   r   imagemath_max   rJ   r   r   r$   c                 C  s   t | j|S r   r   )r   r   r   r   r   imagemath_convert   r   r   )r   r   equalnotequalr0   r   r   
expressionCallable[[dict[str, Any]], Any]optionsdict[str, Any]kwr   c                 K  s~   |rt ddd t }|| || | D ]\}}t|tjr*t|||< q| |}z|jW S  t	y>   | Y S w )a  
    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 options: Values to add to the function's dictionary. Deprecated.
                    You can instead use one or more keyword arguments.
    :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.
    zImageMath.lambda_eval options   z'ImageMath.lambda_eval keyword arguments)
r	   opscopyupdateitemsr   r   r
   r   r,   )r   r   r   argskvr4   r   r   r   lambda_eval   s&   

r   c                   s   |rt ddd t  t| t|  D ]}d|v s#tt|r-d| d}t|q |  |  	 D ]\}}t
|tjrLt| |< q<t| ddd fdd t| ddtii }z|jW S  tyz   | 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 options: Values to add to the evaluation context. Deprecated.
                    You can instead use one or more keyword arguments.
    :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.
    zImageMath.unsafe_eval optionsr   z'ImageMath.unsafe_eval keyword arguments__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   D  s   

zunsafe_eval.<locals>.scan
__builtinsr=   N)r   r   r   r   )r	   r   r   listkeyshasattrbuiltinsr   r   r   r   r   r
   compiler   r=   r   r,   )r   r   r   r   r!   r   r4   r   r   r   unsafe_eval  s6   


r   _dictc                 K  s   t ddd t| |fi |S )a1  
    Evaluates an image expression.

    Deprecated. Use lambda_eval() or unsafe_eval() instead.

    :param expression: A string containing a Python-style expression.
    :param _dict: Values to add to the evaluation context.  You
                  can either use a dictionary, or one or more keyword
                  arguments.
    :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.

    ..  deprecated:: 10.3.0
    zImageMath.evalr   z.ImageMath.lambda_eval or ImageMath.unsafe_eval)r	   r   )r   r   r   r   r   r   r   V  s   r   )r   r
   r   r
   )r   r
   rD   r&   r   r
   )r   r
   r   r$   r   r
   )r   r   r   r   r   r   r   r   )r   r$   r   r   r   r   r   r   )r   r$   r   r   r   r   r   r   )
__future__r   r   typesr   typingr   r    r   r   
_deprecater	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s:    
*





,B