
    g,                        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	      Zdd
ZddZddZddZddZddZddZeeeeeeedZi f	 	 	 	 	 	 	 ddZi f	 	 	 	 	 	 	 ddZi f	 	 	 	 	 	 	 ddZy)    )annotationsN)CodeType)AnyCallable   )Image_imagingmath)	deprecatec                  4   e Zd ZdZd%dZd&dZ	 	 d'	 	 	 	 	 	 	 	 	 d(dZd)dZd*dZd*dZ	d*d	Z
d+d
Zd+dZd+dZd+dZd+dZd+dZd+dZd+dZd+dZd+dZd+dZd+dZd*dZd+dZd+dZd+dZd+dZd+dZd+dZd+dZd+dZd Z d  Z!d+d!Z"d+d"Z#d+d#Z$d+d$Z%y),_Operandz4Wraps an image operand, providing standard operatorsc                    || _         y N)im)selfr   s     B/var/www/openai/venv/lib/python3.12/site-packages/PIL/ImageMath.py__init__z_Operand.__init__   s	        c                    t        |t              r{|j                  j                  dv r|j                  j	                  d      S |j                  j                  dv r|j                  S d|j                  j                   }t        |      t        |t        t        f      rC| j                  j                  dv r+t        j                  d| j                  j                  |      S t        j                  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   im1msgs      r   __fixupz_Operand.__fixup!   s    c8$vv{{j(vv~~c**
*vv*366;;-8 o% #U|,1PyydggllC88yydggllC88r   Nc                   | j                  |      }|t        j                  |xs |j                  |j                  d       }|j                          	 t        t        | d|j                         }t        j                  ||j                  j                  |j                  j                         t#        |      S | j                  |      }	|j                  |	j                  k7  r@|j                  dk7  r|j                  d      }|	j                  dk7  r|	j                  d      }	|j                  |	j                  k7  rt        |j                  d   |	j                  d         t        |j                  d   |	j                  d         f}
|j                  |
k7  r|j                  d|
z         }|	j                  |
k7  r|	j                  d|
z         }	t        j                  |xs |j                  |j                  d       }|j                          |	j                          	 t        t        | d|j                         }t        j                   ||j                  j                  |j                  j                  |	j                  j                         t#        |      S # t        $ r}d| d}t        |      |d }~ww xY w# t        $ r}d| d}t        |      |d }~ww xY w)N_zbad operand type for ''r   r   r   )r   r   )_Operand__fixupr   r   r   r    loadgetattrr	   AttributeError	TypeErrorunopr   idr   mincropbinopr   )r   opr!   im2r   im_1outer"   im_2r    s              r   applyz_Operand.apply3   sO    ||C ;))D-DIItyy$?CIIK,\bT499++>? b#&&))TWWZZ8: }5 <<$DyyDII%99#<<,D99#<<,DyyDII% 		!diil3		!diil3 99$99Vd]3D99$99Vd]3D))D-DIItyy$?CIIKIIK,\bT499++>? r36699dggjj$''**E}A " ,.rd!4n!+,8 " ,.rd!4n!+,s0   J  "K  	K )J;;K 	K#KK#c                :    | j                   j                         d uS r   )r   getbboxr   s    r   __bool__z_Operand.__bool__d   s    ww ,,r   c                &    | j                  d|       S )Nabsr7   r:   s    r   __abs__z_Operand.__abs__h       zz%&&r   c                    | S r    r:   s    r   __pos__z_Operand.__pos__k   s    r   c                &    | j                  d|       S )Nnegr>   r:   s    r   __neg__z_Operand.__neg__n   r@   r   c                (    | j                  d| |      S Naddr>   r   others     r   __add__z_Operand.__add__r       zz%u--r   c                (    | j                  d||       S rH   r>   rJ   s     r   __radd__z_Operand.__radd__u       zz%--r   c                (    | j                  d| |      S Nsubr>   rJ   s     r   __sub__z_Operand.__sub__x   rM   r   c                (    | j                  d||       S rR   r>   rJ   s     r   __rsub__z_Operand.__rsub__{   rP   r   c                (    | j                  d| |      S Nmulr>   rJ   s     r   __mul__z_Operand.__mul__~   rM   r   c                (    | j                  d||       S rX   r>   rJ   s     r   __rmul__z_Operand.__rmul__   rP   r   c                (    | j                  d| |      S Ndivr>   rJ   s     r   __truediv__z_Operand.__truediv__   rM   r   c                (    | j                  d||       S r^   r>   rJ   s     r   __rtruediv__z_Operand.__rtruediv__   rP   r   c                (    | j                  d| |      S Nmodr>   rJ   s     r   __mod__z_Operand.__mod__   rM   r   c                (    | j                  d||       S rd   r>   rJ   s     r   __rmod__z_Operand.__rmod__   rP   r   c                (    | j                  d| |      S Npowr>   rJ   s     r   __pow__z_Operand.__pow__   rM   r   c                (    | j                  d||       S rj   r>   rJ   s     r   __rpow__z_Operand.__rpow__   rP   r   c                &    | j                  d|       S )Ninvertr>   r:   s    r   
__invert__z_Operand.__invert__   s    zz(D))r   c                (    | j                  d| |      S Nandr>   rJ   s     r   __and__z_Operand.__and__   rM   r   c                (    | j                  d||       S rs   r>   rJ   s     r   __rand__z_Operand.__rand__   rP   r   c                (    | j                  d| |      S Norr>   rJ   s     r   __or__z_Operand.__or__       zz$e,,r   c                (    | j                  d||       S ry   r>   rJ   s     r   __ror__z_Operand.__ror__   s    zz$t,,r   c                (    | j                  d| |      S Nxorr>   rJ   s     r   __xor__z_Operand.__xor__   rM   r   c                (    | j                  d||       S r   r>   rJ   s     r   __rxor__z_Operand.__rxor__   rP   r   c                (    | j                  d| |      S )Nlshiftr>   rJ   s     r   
__lshift__z_Operand.__lshift__       zz(D%00r   c                (    | j                  d| |      S )Nrshiftr>   rJ   s     r   
__rshift__z_Operand.__rshift__   r   r   c                (    | j                  d| |      S )Neqr>   rJ   s     r   __eq__z_Operand.__eq__   r|   r   c                (    | j                  d| |      S )Nner>   rJ   s     r   __ne__z_Operand.__ne__   r|   r   c                (    | j                  d| |      S )Nltr>   rJ   s     r   __lt__z_Operand.__lt__   r|   r   c                (    | j                  d| |      S )Nler>   rJ   s     r   __le__z_Operand.__le__   r|   r   c                (    | j                  d| |      S )Ngtr>   rJ   s     r   __gt__z_Operand.__gt__   r|   r   c                (    | j                  d| |      S )Nger>   rJ   s     r   __ge__z_Operand.__ge__   r|   r   )r   Image.Image)r!   _Operand | floatreturnr   )NN)
r1   strr!   r   r2   _Operand | float | Noner   z
str | Noner   r   )r   bool)r   r   )rK   r   r   r   )&__name__
__module____qualname____doc__r   r'   r7   r;   r?   rC   rF   rL   rO   rT   rV   rZ   r\   r`   rb   rf   rh   rl   rn   rq   ru   rw   r{   r~   r   r   r   r   r   r   r   r   r   r   rB   r   r   r   r      s    >9, (,.. . %	.
 . 
.b-''............*..--..11------r   r   c                J    t        | j                  j                  d            S )Nr   r   r   r   r:   s    r   imagemath_intr          DGGOOC())r   c                J    t        | j                  j                  d            S )Nr   r   r:   s    r   imagemath_floatr      r   r   c                ,    | j                  d| |d      S )Nr   r   r   r>   rJ   s     r   imagemath_equalr          ::dD%c:22r   c                ,    | j                  d| |d      S )Nr   r   r   r>   rJ   s     r   imagemath_notequalr      r   r   c                (    | j                  d| |      S )Nr.   r>   rJ   s     r   imagemath_minr          ::eT5))r   c                (    | j                  d| |      S )Nmaxr>   rJ   s     r   imagemath_maxr      r   r   c                J    t        | j                  j                  |            S r   r   )r   r   s     r   imagemath_convertr      s    DGGOOD)**r   )r   r   equalnotequalr.   r   r   c                    t         j                         }|j                  |       |j                  |       |j                         D ]   \  }}t	        |d      st        |      ||<   "  | |      }	 |j                  S # t        $ r |cY S w xY 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. You
                    can either use a dictionary, or one or more keyword
                    arguments.
    :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.
    r   )opscopyupdateitemshasattrr   r   r*   )
expressionoptionskwargskvr4   s          r   lambda_evalr      s    * 88:DKKKKO

11dqkDG  T
Cvv 
s   3A? ?BBc                H  	 t         j                         t        |j                               t        |j                               z   D ]'  }d|v st	        t
        |      sd| d}t        |       j                  |       j                  |       j                         D ]   \  }}t	        |d      st        |      |<   " t        | dd      d
	fd	 	       t        j                  | dd	t        ii      }	 |j                  S # t        $ r |cY S w xY 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.  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.
    __r&   ' not allowedr   z<string>evalc                    | j                   D ]!  }t        |      t              u s |       # | j                  D ]  }|vs|dk7  sd| d}t        |       y )Nr=   r&   r   )	co_conststypeco_namesr   )codeconstnamer"   r   compiled_codescans       r   r   zunsafe_eval.<locals>.scan9  s_    ^^EE{d=11U $ MMD4DEM$}- o% "r   
__builtinsr=   )r   r   r   None)r   r   listkeysr   builtinsr   r   r   r   compiler   r=   r   r*   )
r   r   r   r   r"   r   r4   r   r   r   s
          @@@r   unsafe_evalr     s    2 88:D',,.!DO319!,aS&CS/! 4
 	KKKKO

11dqkDG  J
F;M& 	
--
\E3<$@$
GCvv 
s   D D! D!c                6    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.eval   z.ImageMath.lambda_eval or ImageMath.unsafe_eval)r
   r   )r   _dictr   s      r   r   r   K  s)    * 
8
 z5/B//r   )r   r   r   r   )r   r   rK   r   r   r   )r   r   r   r   r   r   )r   zCallable[[dict[str, Any]], Any]r   dict[str, Any]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   rB   r   r   <module>r      s   " #     ! !h- h-X**
33**+
 "  ! /     		 J !666 6 		6v 000 0 		0r   