
     f,                        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dS )(    )annotationsN)CodeType)AnyCallable   )Image_imagingmath)	deprecatec                  $   e Zd ZdZd2dZd3dZ	 	 d4d5dZd6dZd7dZd7dZ	d7dZ
d8dZd8dZd8dZd8dZd8dZd8dZd8dZd8dZd8dZd8d Zd8d!Zd8d"Zd7d#Zd8d$Zd8d%Zd8d&Zd8d'Zd8d(Zd8d)Zd8d*Zd8d+Zd, Z d- Z!d8d.Z"d8d/Z#d8d0Z$d8d1Z%d	S )9_Operandz4Wraps an image operand, providing standard operatorsimImage.Imagec                    || _         d S N)r   )selfr   s     P/var/www/api.educacionweb.es/myenv/lib/python3.11/site-packages/PIL/ImageMath.py__init__z_Operand.__init__   s        im1_Operand | floatreturnc                   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          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   r   msgs      r   __fixupz_Operand.__fixup!   s    c8$$ 	9v{j((v~~c***
**v836;88 oo% #U|,, 91P1PydglC888ydglC888r   Nopstrim2_Operand | float | Noner   
str | Nonec                B   |                      |          }|t          j        |p|j        |j        d           }|                                 	 t          t          | d|j                   }n(# t          $ r}d| d}t          |          |d }~ww xY wt          j
        ||j        j        |j        j                   n|                      |          }	|j        |	j        k    r@|j        dk    r|                    d          }|	j        dk    r|	                    d          }	|j        |	j        k    rt          |j        d         |	j        d                   t          |j        d         |	j        d                   f}
|j        |
k    r|                    d|
z             }|	j        |
k    r|	                    d|
z             }	t          j        |p|j        |j        d           }|                                 |	                                 	 t          t          | d|j                   }n(# t          $ r}d| d}t          |          |d }~ww xY wt          j        ||j        j        |j        j        |	j        j                   t#          |          S )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   r'   r   r)   r   im_1outer%   im_2r$   s              r   applyz_Operand.apply3   s    ||C  ;)D-DIty$??CIIKKK,\b+>+>49+>+>??! , , ,4r444nn!+, b#&)TWZ8888 <<$$DyDI%%9##<<,,D9##<<,,DyDI%% 	!dil33	!dil33 9$$99Vd]33D9$$99Vd]33D)D-DIty$??CIIKKKIIKKK,\b+>+>49+>+>??! , , ,4r444nn!+, r369dgj$'*EEE}}s0   A/ /
B9BBH5 5
I?IIboolc                8    | j                                         d uS r   )r   getbboxr   s    r   __bool__z_Operand.__bool__d   s    w  ,,r   c                .    |                      d|           S )Nabsr=   rA   s    r   __abs__z_Operand.__abs__h       zz%&&&r   c                    | S r    rA   s    r   __pos__z_Operand.__pos__k   s    r   c                .    |                      d|           S )NnegrE   rA   s    r   __neg__z_Operand.__neg__n   rG   r   otherc                0    |                      d| |          S NaddrE   r   rN   s     r   __add__z_Operand.__add__r       zz%u---r   c                0    |                      d||           S rP   rE   rR   s     r   __radd__z_Operand.__radd__u       zz%---r   c                0    |                      d| |          S NsubrE   rR   s     r   __sub__z_Operand.__sub__x   rT   r   c                0    |                      d||           S rY   rE   rR   s     r   __rsub__z_Operand.__rsub__{   rW   r   c                0    |                      d| |          S NmulrE   rR   s     r   __mul__z_Operand.__mul__~   rT   r   c                0    |                      d||           S r_   rE   rR   s     r   __rmul__z_Operand.__rmul__   rW   r   c                0    |                      d| |          S NdivrE   rR   s     r   __truediv__z_Operand.__truediv__   rT   r   c                0    |                      d||           S re   rE   rR   s     r   __rtruediv__z_Operand.__rtruediv__   rW   r   c                0    |                      d| |          S NmodrE   rR   s     r   __mod__z_Operand.__mod__   rT   r   c                0    |                      d||           S rk   rE   rR   s     r   __rmod__z_Operand.__rmod__   rW   r   c                0    |                      d| |          S NpowrE   rR   s     r   __pow__z_Operand.__pow__   rT   r   c                0    |                      d||           S rq   rE   rR   s     r   __rpow__z_Operand.__rpow__   rW   r   c                .    |                      d|           S )NinvertrE   rA   s    r   
__invert__z_Operand.__invert__   s    zz(D)))r   c                0    |                      d| |          S NandrE   rR   s     r   __and__z_Operand.__and__   rT   r   c                0    |                      d||           S rz   rE   rR   s     r   __rand__z_Operand.__rand__   rW   r   c                0    |                      d| |          S NorrE   rR   s     r   __or__z_Operand.__or__       zz$e,,,r   c                0    |                      d||           S r   rE   rR   s     r   __ror__z_Operand.__ror__   s    zz$t,,,r   c                0    |                      d| |          S NxorrE   rR   s     r   __xor__z_Operand.__xor__   rT   r   c                0    |                      d||           S r   rE   rR   s     r   __rxor__z_Operand.__rxor__   rW   r   c                0    |                      d| |          S )NlshiftrE   rR   s     r   
__lshift__z_Operand.__lshift__       zz(D%000r   c                0    |                      d| |          S )NrshiftrE   rR   s     r   
__rshift__z_Operand.__rshift__   r   r   c                0    |                      d| |          S )NeqrE   rR   s     r   __eq__z_Operand.__eq__   r   r   c                0    |                      d| |          S )NnerE   rR   s     r   __ne__z_Operand.__ne__   r   r   c                0    |                      d| |          S )NltrE   rR   s     r   __lt__z_Operand.__lt__   r   r   c                0    |                      d| |          S )NlerE   rR   s     r   __le__z_Operand.__le__   r   r   c                0    |                      d| |          S )NgtrE   rR   s     r   __gt__z_Operand.__gt__   r   r   c                0    |                      d| |          S )NgerE   rR   s     r   __ge__z_Operand.__ge__   r   r   )r   r   )r   r   r   r   )NN)
r'   r(   r   r   r)   r*   r   r+   r   r   )r   r>   )r   r   )rN   r   r   r   )&__name__
__module____qualname____doc__r   r/   r=   rB   rF   rJ   rM   rS   rV   r[   r]   ra   rc   rg   ri   rm   ro   rs   ru   rx   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   rI   r   r   r   r      s       >>   9 9 9 9, (,. . . . .b- - - -' ' ' '   ' ' ' '. . . .. . . .. . . .. . . .. . . .. . . .. . . .. . . .. . . .. . . .. . . .. . . .* * * *. . . .. . . .- - - -- - - -. . . .. . . .1 1 1 11 1 1 1- - -- - -- - - -- - - -- - - -- - - - - -r   r   r   r   c                P    t          | j                            d                    S )Nr   r   r   r   rA   s    r   imagemath_intr          DGOOC(()))r   c                P    t          | j                            d                    S )Nr   r   rA   s    r   imagemath_floatr      r   r   rN   r*   c                4    |                      d| |d          S )Nr   r   r   rE   rR   s     r   imagemath_equalr          ::dD%c:222r   c                4    |                      d| |d          S )Nr   r   r   rE   rR   s     r   imagemath_notequalr      r   r   c                0    |                      d| |          S )Nr6   rE   rR   s     r   imagemath_minr          ::eT5)))r   c                0    |                      d| |          S )NmaxrE   rR   s     r   imagemath_maxr      r   r   r   r(   c                P    t          | j                            |                    S r   r   )r   r   s     r   imagemath_convertr      s    DGOOD))***r   )r!   r"   equalnotequalr6   r   r   
expressionCallable[[dict[str, Any]], Any]optionsdict[str, Any]kwr   c                J   t                                           }|                    |           |                    |           |                                D ]'\  }}t	          |d          r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   r2   )r   r   r   argskvr:   s          r   lambda_evalr      s    * 88::DKKKKOOO

 " "11d 	"qkkDG
*T

Cv   


s   B B"!B"c                  	 t                                           t          |                                          t          |                                          z   D ]0}d|v st	          t
          |          rd| d}t          |          1                    |                               |                                           D ]'\  }}t	          |d          r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>evalcoder   r   Nonec                    | j         D ]+}t          |          t                    u r |           ,| j        D ]!}|vr|dk    rd| d}t          |          "d S )NrD   r.   r   )	co_conststypeco_namesr    )r   constnamer%   r   compiled_codescans       r   r   zunsafe_eval.<locals>.scan9  s    ^ 	 	EE{{d=1111UM 	& 	&D4DEMM-$--- oo%	& 	&r   
__builtinsrD   N)r   r   r   r   )r   r   listkeysr   builtinsr    r   r   r   compiler   rD   r   r2   )
r   r   r   r   r%   r   r:   r   r   r   s
          @@@r   unsafe_evalr     su   2 88::D',,..!!DOO3 " "199!,,9&a&&&CS//!  	KKKKOOO

 " "11d 	"qkkDGJ
F;;M& & & & & & & & 	D
-
\E3<$@$
G
GCv   


s   ;E EE_dictc                @    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   r   r   s      r   r   r   K  s9    * 
8  
 z5//B///r   )r   r   r   r   )r   r   rN   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   rI   r   r   <module>r      s  " # " " " " "                        ! ! ! ! ! ! ! ! ! ! ! ! ! !h- h- h- h- h- h- h- h-X* * * ** * * *
3 3 3 33 3 3 3* * * ** * * *+ + + +
 "   !         J !6 6 6 6 6v 0 0 0 0 0 0 0r   