a :jg.@s4ddlmZddlZddlmZddlmZmZddlm Z m Z ddl m Z Gdd d Z d d d d d Zd d d d dZd dd dddZd dd dddZd dd dddZd dd dddZd dd dddZeeeeeeedZifddd d d!d"d#Zifddd d d!d$d%Zifddd d d&d'd(ZdS))) annotationsN)CodeType)AnyCallable)Image _imagingmath) deprecatec@s,eZdZdZddddZddddd ZdRd dd d ddddZddddZddddZddddZ ddddZ dddddZ dddddZ dddd d!Z dddd"d#Zdddd$d%Zdddd&d'Zdddd(d)Zdddd*d+Zdddd,d-Zdddd.d/Zdddd0d1Zdddd2d3Zddd4d5Zdddd6d7Zdddd8d9Zdddd:d;Zddddd?Zdddd@dAZddddBdCZddddDdEZddddFdGZ ddddHdIZ!ddddJdKZ"ddddLdMZ#ddddNdOZ$ddddPdQZ%d S)S_Operandz4Wraps an image operand, providing standard operatorsz Image.ImageimcCs ||_dSNr )selfr r7/usr/local/lib/python3.9/site-packages/PIL/ImageMath.py__init__sz_Operand.__init__z_Operand | float)im1returncCst|trL|jjdvr"|jdS|jjdvr4|jSd|jj}t|n>t|ttfrx|jjdvrxt d|jj |St d|jj |SdS)N)1LI)rFzunsupported mode: )rrrr) isinstancer r modeconvert ValueErrorintfloatrnewsize)rrmsgrrrZ__fixup!s     z_Operand.__fixupNstr_Operand | float | Nonez str | None)oprim2rrc Cs||}|durt|p|j|jd}ztt|d|j}Wn:ty|}z"d|d}t||WYd}~n d}~00t || | n8||} |j| jkr|jdkr| d}| jdkr| d} |j| jkrDt |jd| jdt |jd| jdf} |j| kr*| d| }| j| krD| d| } t|pR|j|jd}ztt|d|j}Wn<ty}z"d|d}t||WYd}~n d}~00t|| | | t|S)N_zbad operand type for ''rrr)rr)_Operand__fixuprrrrgetattrrAttributeError TypeErrorunopZgetimrmincropbinopr ) rr#rr$rZim_1outer Zim_2rrrrapply3s>             z_Operand.applybool)rcCs|jduSr )r Zgetbboxrrrr__bool__asz_Operand.__bool__cCs |d|S)Nabsr1r3rrr__abs__esz_Operand.__abs__cCs|Sr rr3rrr__pos__hsz_Operand.__pos__cCs |d|S)Nnegr6r3rrr__neg__ksz_Operand.__neg__)otherrcCs|d||SNaddr6rr;rrr__add__osz_Operand.__add__cCs|d||Sr<r6r>rrr__radd__rsz_Operand.__radd__cCs|d||SNsubr6r>rrr__sub__usz_Operand.__sub__cCs|d||SrAr6r>rrr__rsub__xsz_Operand.__rsub__cCs|d||SNmulr6r>rrr__mul__{sz_Operand.__mul__cCs|d||SrEr6r>rrr__rmul__~sz_Operand.__rmul__cCs|d||SNdivr6r>rrr __truediv__sz_Operand.__truediv__cCs|d||SrIr6r>rrr __rtruediv__sz_Operand.__rtruediv__cCs|d||SNmodr6r>rrr__mod__sz_Operand.__mod__cCs|d||SrMr6r>rrr__rmod__sz_Operand.__rmod__cCs|d||SNpowr6r>rrr__pow__sz_Operand.__pow__cCs|d||SrQr6r>rrr__rpow__sz_Operand.__rpow__cCs |d|S)Ninvertr6r3rrr __invert__sz_Operand.__invert__cCs|d||SNandr6r>rrr__and__sz_Operand.__and__cCs|d||SrWr6r>rrr__rand__sz_Operand.__rand__cCs|d||SNorr6r>rrr__or__sz_Operand.__or__cCs|d||Sr[r6r>rrr__ror__sz_Operand.__ror__cCs|d||SNxorr6r>rrr__xor__sz_Operand.__xor__cCs|d||Sr_r6r>rrr__rxor__sz_Operand.__rxor__cCs|d||S)Nlshiftr6r>rrr __lshift__sz_Operand.__lshift__cCs|d||S)Nrshiftr6r>rrr __rshift__sz_Operand.__rshift__cCs|d||S)Neqr6r>rrr__eq__sz_Operand.__eq__cCs|d||S)Nner6r>rrr__ne__sz_Operand.__ne__cCs|d||S)Nltr6r>rrr__lt__sz_Operand.__lt__cCs|d||S)Nler6r>rrr__le__sz_Operand.__le__cCs|d||S)Ngtr6r>rrr__gt__sz_Operand.__gt__cCs|d||S)Nger6r>rrr__ge__sz_Operand.__ge__)NN)&__name__ __module__ __qualname____doc__rr'r1r4r7r8r:r?r@rCrDrGrHrKrLrOrPrSrTrVrYrZr]r^rarbrdrfrhrjrlrnrprrrrrrr sJ.r )rrcCst|jdS)Nrr r rr3rrr imagemath_intsrxcCst|jdS)Nrrwr3rrrimagemath_floatsryr")rr;rcCs|jd||ddS)Nrgrrr6r>rrrimagemath_equalsr{cCs|jd||ddS)Nrirrzr6r>rrrimagemath_notequalsr|cCs|d||S)Nr,r6r>rrr imagemath_minsr}cCs|d||S)Nmaxr6r>rrr imagemath_maxsrr!)rrrcCst|j|Sr rw)rrrrrimagemath_convertsr)rrequalZnotequalr,r~rzCallable[[dict[str, Any]], Any]zdict[str, Any]r) expressionoptionskwrcKs|rtdddt}|||||D] \}}t|tjr4t|||<q4||}z|jWSt y||YS0dS)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. zImageMath.lambda_eval options z'ImageMath.lambda_eval keyword argumentsN) r opscopyupdateitemsrrr r r))rrrargskvr/rrr lambda_evals"    rc s|rtdddtt|t|D]*}d|vsFtt|r0d|d}t|q0|| D] \}}t |t j rxt ||<qxt |ddd d d fd d t|ddtii}z|jWSty|YS0dS)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. zImageMath.unsafe_eval optionsrz'ImageMath.unsafe_eval keyword arguments__r& ' not allowedzevalrNone)codercsX|jD]}t|tur|q|jD](}|vr*|dkr*d|d}t|q*dS)Nr5r&r) co_conststypeco_namesr)rconstnamer rZ compiled_codescanrrrDs    zunsafe_eval..scanZ __builtinsr5N)r rrlistkeyshasattrbuiltinsrrrrrr compilerr5r r))rrrrr rr/rrr unsafe_evals0       r)r_dictrrcKstdddt||fi|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 zImageMath.evalrz.ImageMath.lambda_eval or ImageMath.unsafe_eval)r r)rrrrrrrVs r) __future__rrtypesrtypingrrrrZ _deprecater r rxryr{r|r}rrrrrrrrrrs8   * ,B