Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return boolean from ImageMath lambda_eval and unsafe_eval comparison operations #8227

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 57 additions & 146 deletions Tests/test_imagemath_lambda_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ def test_compare() -> None:
)
== "I 2"
)
assert pixel(ImageMath.lambda_eval(lambda args: args["A"] == 1, **images)) == "I 1"
assert pixel(ImageMath.lambda_eval(lambda args: args["A"] == 2, **images)) == "I 0"
for i in range(1, 3):
assert ImageMath.lambda_eval(lambda args: args["A"] == i, **images) is False
assert ImageMath.lambda_eval(lambda args: args["A"] != i, **images) is True


def test_one_image_larger() -> None:
Expand Down Expand Up @@ -310,198 +311,108 @@ def test_bitwise_rightshift() -> None:


def test_logical_eq() -> None:
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] == args["A"], A=A)) == "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] == args["B"], B=B)) == "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] == args["B"], A=A, B=B))
== "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] == args["A"], A=A, B=B))
== "I 0"
)
assert ImageMath.lambda_eval(lambda args: args["A"] == args["A"], A=A) is True
assert ImageMath.lambda_eval(lambda args: args["B"] == args["B"], B=B) is True
assert ImageMath.lambda_eval(lambda args: args["A"] == args["B"], A=A, B=B) is False
assert ImageMath.lambda_eval(lambda args: args["B"] == args["A"], A=A, B=B) is False


def test_logical_ne() -> None:
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] != args["A"], A=A)) == "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] != args["B"], B=B)) == "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] != args["B"], A=A, B=B))
== "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] != args["A"], A=A, B=B))
== "I 1"
)
assert ImageMath.lambda_eval(lambda args: args["A"] != args["A"], A=A) is False
assert ImageMath.lambda_eval(lambda args: args["B"] != args["B"], B=B) is False
assert ImageMath.lambda_eval(lambda args: args["A"] != args["B"], A=A, B=B) is True
assert ImageMath.lambda_eval(lambda args: args["B"] != args["A"], A=A, B=B) is True


def test_logical_lt() -> None:
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] < args["A"], A=A)) == "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] < args["B"], B=B)) == "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] < args["B"], A=A, B=B))
== "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] < args["A"], A=A, B=B))
== "I 0"
)
assert ImageMath.lambda_eval(lambda args: args["A"] < args["A"], A=A) is False
assert ImageMath.lambda_eval(lambda args: args["B"] < args["B"], B=B) is False
assert ImageMath.lambda_eval(lambda args: args["A"] < args["B"], A=A, B=B) is True
assert ImageMath.lambda_eval(lambda args: args["B"] < args["A"], A=A, B=B) is False


def test_logical_le() -> None:
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] <= args["A"], A=A)) == "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] <= args["B"], B=B)) == "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] <= args["B"], A=A, B=B))
== "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] <= args["A"], A=A, B=B))
== "I 0"
)
assert ImageMath.lambda_eval(lambda args: args["A"] <= args["A"], A=A) is True
assert ImageMath.lambda_eval(lambda args: args["B"] <= args["B"], B=B) is True
assert ImageMath.lambda_eval(lambda args: args["A"] <= args["B"], A=A, B=B) is True
assert ImageMath.lambda_eval(lambda args: args["B"] <= args["A"], A=A, B=B) is False


def test_logical_gt() -> None:
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] > args["A"], A=A)) == "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] > args["B"], B=B)) == "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] > args["B"], A=A, B=B))
== "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] > args["A"], A=A, B=B))
== "I 1"
)
assert ImageMath.lambda_eval(lambda args: args["A"] > args["A"], A=A) is False
assert ImageMath.lambda_eval(lambda args: args["B"] > args["B"], B=B) is False
assert ImageMath.lambda_eval(lambda args: args["A"] > args["B"], A=A, B=B) is False
assert ImageMath.lambda_eval(lambda args: args["B"] > args["A"], A=A, B=B) is True


def test_logical_ge() -> None:
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] >= args["A"], A=A)) == "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] >= args["B"], B=B)) == "I 1"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["A"] >= args["B"], A=A, B=B))
== "I 0"
)
assert (
pixel(ImageMath.lambda_eval(lambda args: args["B"] >= args["A"], A=A, B=B))
== "I 1"
)
assert ImageMath.lambda_eval(lambda args: args["A"] >= args["A"], A=A) is True
assert ImageMath.lambda_eval(lambda args: args["B"] >= args["B"], B=B) is True
assert ImageMath.lambda_eval(lambda args: args["A"] >= args["B"], A=A, B=B) is False
assert ImageMath.lambda_eval(lambda args: args["B"] >= args["A"], A=A, B=B) is True


def test_logical_equal() -> None:
assert (
pixel(
ImageMath.lambda_eval(lambda args: args["equal"](args["A"], args["A"]), A=A)
)
== "I 1"
ImageMath.lambda_eval(lambda args: args["equal"](args["A"], args["A"]), A=A)
is True
)
assert (
pixel(
ImageMath.lambda_eval(lambda args: args["equal"](args["B"], args["B"]), B=B)
)
== "I 1"
ImageMath.lambda_eval(lambda args: args["equal"](args["B"], args["B"]), B=B)
is True
)
assert (
pixel(
ImageMath.lambda_eval(lambda args: args["equal"](args["Z"], args["Z"]), Z=Z)
)
== "I 1"
ImageMath.lambda_eval(lambda args: args["equal"](args["Z"], args["Z"]), Z=Z)
is True
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["equal"](args["A"], args["B"]), A=A, B=B
)
ImageMath.lambda_eval(
lambda args: args["equal"](args["A"], args["B"]), A=A, B=B
)
== "I 0"
is False
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["equal"](args["B"], args["A"]), A=A, B=B
)
ImageMath.lambda_eval(
lambda args: args["equal"](args["B"], args["A"]), A=A, B=B
)
== "I 0"
is False
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["equal"](args["A"], args["Z"]), A=A, Z=Z
)
ImageMath.lambda_eval(
lambda args: args["equal"](args["A"], args["Z"]), A=A, Z=Z
)
== "I 0"
is False
)


def test_logical_not_equal() -> None:
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["notequal"](args["A"], args["A"]), A=A
)
)
== "I 0"
ImageMath.lambda_eval(lambda args: args["notequal"](args["A"], args["A"]), A=A)
is False
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["notequal"](args["B"], args["B"]), B=B
)
)
== "I 0"
ImageMath.lambda_eval(lambda args: args["notequal"](args["B"], args["B"]), B=B)
is False
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["notequal"](args["Z"], args["Z"]), Z=Z
)
)
== "I 0"
ImageMath.lambda_eval(lambda args: args["notequal"](args["Z"], args["Z"]), Z=Z)
is False
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["notequal"](args["A"], args["B"]), A=A, B=B
)
ImageMath.lambda_eval(
lambda args: args["notequal"](args["A"], args["B"]), A=A, B=B
)
== "I 1"
is True
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["notequal"](args["B"], args["A"]), A=A, B=B
)
ImageMath.lambda_eval(
lambda args: args["notequal"](args["B"], args["A"]), A=A, B=B
)
== "I 1"
is True
)
assert (
pixel(
ImageMath.lambda_eval(
lambda args: args["notequal"](args["A"], args["Z"]), A=A, Z=Z
)
ImageMath.lambda_eval(
lambda args: args["notequal"](args["A"], args["Z"]), A=A, Z=Z
)
== "I 1"
is True
)
76 changes: 38 additions & 38 deletions Tests/test_imagemath_unsafe_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def test_convert() -> None:
def test_compare() -> None:
assert pixel(ImageMath.unsafe_eval("min(A, B)", **images)) == "I 1"
assert pixel(ImageMath.unsafe_eval("max(A, B)", **images)) == "I 2"
assert pixel(ImageMath.unsafe_eval("A == 1", **images)) == "I 1"
assert pixel(ImageMath.unsafe_eval("A == 2", **images)) == "I 0"
assert ImageMath.unsafe_eval("A == 1", **images) is False
assert ImageMath.unsafe_eval("A == 2", **images) is False


def test_one_image_larger() -> None:
Expand Down Expand Up @@ -169,60 +169,60 @@ def test_bitwise_rightshift() -> None:


def test_logical_eq() -> None:
assert pixel(ImageMath.unsafe_eval("A==A", A=A)) == "I 1"
assert pixel(ImageMath.unsafe_eval("B==B", B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("A==B", A=A, B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("B==A", A=A, B=B)) == "I 0"
assert ImageMath.unsafe_eval("A==A", A=A) is True
assert ImageMath.unsafe_eval("B==B", B=B) is True
assert ImageMath.unsafe_eval("A==B", A=A, B=B) is False
assert ImageMath.unsafe_eval("B==A", A=A, B=B) is False


def test_logical_ne() -> None:
assert pixel(ImageMath.unsafe_eval("A!=A", A=A)) == "I 0"
assert pixel(ImageMath.unsafe_eval("B!=B", B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("A!=B", A=A, B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("B!=A", A=A, B=B)) == "I 1"
assert ImageMath.unsafe_eval("A!=A", A=A) is False
assert ImageMath.unsafe_eval("B!=B", B=B) is False
assert ImageMath.unsafe_eval("A!=B", A=A, B=B) is True
assert ImageMath.unsafe_eval("B!=A", A=A, B=B) is True


def test_logical_lt() -> None:
assert pixel(ImageMath.unsafe_eval("A<A", A=A)) == "I 0"
assert pixel(ImageMath.unsafe_eval("B<B", B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("A<B", A=A, B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("B<A", A=A, B=B)) == "I 0"
assert ImageMath.unsafe_eval("A<A", A=A) is False
assert ImageMath.unsafe_eval("B<B", B=B) is False
assert ImageMath.unsafe_eval("A<B", A=A, B=B) is True
assert ImageMath.unsafe_eval("B<A", A=A, B=B) is False


def test_logical_le() -> None:
assert pixel(ImageMath.unsafe_eval("A<=A", A=A)) == "I 1"
assert pixel(ImageMath.unsafe_eval("B<=B", B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("A<=B", A=A, B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("B<=A", A=A, B=B)) == "I 0"
assert ImageMath.unsafe_eval("A<=A", A=A) is True
assert ImageMath.unsafe_eval("B<=B", B=B) is True
assert ImageMath.unsafe_eval("A<=B", A=A, B=B) is True
assert ImageMath.unsafe_eval("B<=A", A=A, B=B) is False


def test_logical_gt() -> None:
assert pixel(ImageMath.unsafe_eval("A>A", A=A)) == "I 0"
assert pixel(ImageMath.unsafe_eval("B>B", B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("A>B", A=A, B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("B>A", A=A, B=B)) == "I 1"
assert ImageMath.unsafe_eval("A>A", A=A) is False
assert ImageMath.unsafe_eval("B>B", B=B) is False
assert ImageMath.unsafe_eval("A>B", A=A, B=B) is False
assert ImageMath.unsafe_eval("B>A", A=A, B=B) is True


def test_logical_ge() -> None:
assert pixel(ImageMath.unsafe_eval("A>=A", A=A)) == "I 1"
assert pixel(ImageMath.unsafe_eval("B>=B", B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("A>=B", A=A, B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("B>=A", A=A, B=B)) == "I 1"
assert ImageMath.unsafe_eval("A>=A", A=A) is True
assert ImageMath.unsafe_eval("B>=B", B=B) is True
assert ImageMath.unsafe_eval("A>=B", A=A, B=B) is False
assert ImageMath.unsafe_eval("B>=A", A=A, B=B) is True


def test_logical_equal() -> None:
assert pixel(ImageMath.unsafe_eval("equal(A, A)", A=A)) == "I 1"
assert pixel(ImageMath.unsafe_eval("equal(B, B)", B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("equal(Z, Z)", Z=Z)) == "I 1"
assert pixel(ImageMath.unsafe_eval("equal(A, B)", A=A, B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("equal(B, A)", A=A, B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("equal(A, Z)", A=A, Z=Z)) == "I 0"
assert ImageMath.unsafe_eval("equal(A, A)", A=A) is True
assert ImageMath.unsafe_eval("equal(B, B)", B=B) is True
assert ImageMath.unsafe_eval("equal(Z, Z)", Z=Z) is True
assert ImageMath.unsafe_eval("equal(A, B)", A=A, B=B) is False
assert ImageMath.unsafe_eval("equal(B, A)", A=A, B=B) is False
assert ImageMath.unsafe_eval("equal(A, Z)", A=A, Z=Z) is False


def test_logical_not_equal() -> None:
assert pixel(ImageMath.unsafe_eval("notequal(A, A)", A=A)) == "I 0"
assert pixel(ImageMath.unsafe_eval("notequal(B, B)", B=B)) == "I 0"
assert pixel(ImageMath.unsafe_eval("notequal(Z, Z)", Z=Z)) == "I 0"
assert pixel(ImageMath.unsafe_eval("notequal(A, B)", A=A, B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("notequal(B, A)", A=A, B=B)) == "I 1"
assert pixel(ImageMath.unsafe_eval("notequal(A, Z)", A=A, Z=Z)) == "I 1"
assert ImageMath.unsafe_eval("notequal(A, A)", A=A) is False
assert ImageMath.unsafe_eval("notequal(B, B)", B=B) is False
assert ImageMath.unsafe_eval("notequal(Z, Z)", Z=Z) is False
assert ImageMath.unsafe_eval("notequal(A, B)", A=A, B=B) is True
assert ImageMath.unsafe_eval("notequal(B, A)", A=A, B=B) is True
assert ImageMath.unsafe_eval("notequal(A, Z)", A=A, Z=Z) is True
4 changes: 2 additions & 2 deletions docs/reference/ImageMath.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Example: Using the :py:mod:`~PIL.ImageMath` module
shown in the above example.
:param \**kw: Values to add to the function's dictionary, mapping image names to
Image instances.
:return: An image, an integer value, a floating point value,
:return: An image, an integer value, a floating point value, a boolean,
or a pixel tuple, depending on the expression.

.. py:function:: unsafe_eval(expression, options, **kw)
Expand All @@ -68,7 +68,7 @@ Example: Using the :py:mod:`~PIL.ImageMath` module
shown in the above example.
:param \**kw: Values to add to the evaluation context, mapping image names to Image
instances.
:return: An image, an integer value, a floating point value,
:return: An image, an integer value, a floating point value, a boolean,
or a pixel tuple, depending on the expression.

Expression syntax
Expand Down
Loading
Loading