Skip to content

Commit

Permalink
Removed unused import and restored existing checks (#1)
Browse files Browse the repository at this point in the history
* Removed unused import

* Restored existing checks

* Restored coerce_e, _E and data property

* Deprecated coerce_e

Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
  • Loading branch information
2 people authored and benrg committed May 3, 2022
1 parent 4e12ccc commit 46802d5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
9 changes: 9 additions & 0 deletions Tests/test_image_point.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from PIL import Image

from .helper import assert_image_equal, hopper


Expand All @@ -17,13 +19,15 @@ def test_sanity():
im.point(list(range(256)))
im.point(lambda x: x * 1)
im.point(lambda x: x + 1)
im.point(lambda x: x - 1)
im.point(lambda x: x * 1 + 1)
im.point(lambda x: 0.1 + 0.2 * x)
im.point(lambda x: -x)
im.point(lambda x: x - 0.5)
im.point(lambda x: 1 - x / 2)
im.point(lambda x: (2 + x) / 3)
im.point(lambda x: 0.5)
im.point(lambda x: x / 1)
with pytest.raises(TypeError):
im.point(lambda x: x * x)
with pytest.raises(TypeError):
Expand Down Expand Up @@ -55,3 +59,8 @@ def test_f_mode():
im = hopper("F")
with pytest.raises(ValueError):
im.point(None)


def test_coerce_e_deprecation():
with pytest.warns(DeprecationWarning):
assert Image.coerce_e(2).data == 2
35 changes: 19 additions & 16 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io
import logging
import math
import numbers
import os
import re
import struct
Expand Down Expand Up @@ -431,19 +430,23 @@ def _getencoder(mode, encoder_name, args, extra=()):
# Simple expression analyzer


# _Affine(m, b) represents the polynomial m x + b
class _Affine:
def __init__(self, m, b):
self.m = m
self.b = b
def coerce_e(value):
deprecate("coerce_e", 10)
return value if isinstance(value, _E) else _E(1, value)


class _E:
def __init__(self, scale, data):
self.scale = scale
self.data = data

def __neg__(self):
return _Affine(-self.m, -self.b)
return _E(-self.scale, -self.data)

def __add__(self, other):
if isinstance(other, _Affine):
return _Affine(self.m + other.m, self.b + other.b)
return _Affine(self.m, self.b + other)
if isinstance(other, _E):
return _E(self.scale + other.scale, self.data + other.data)
return _E(self.scale, self.data + other)

__radd__ = __add__

Expand All @@ -454,21 +457,21 @@ def __rsub__(self, other):
return other + -self

def __mul__(self, other):
if isinstance(other, _Affine):
if isinstance(other, _E):
return NotImplemented
return _Affine(self.m * other, self.b * other)
return _E(self.scale * other, self.data * other)

__rmul__ = __mul__

def __truediv__(self, other):
if isinstance(other, _Affine):
if isinstance(other, _E):
return NotImplemented
return _Affine(self.m / other, self.b / other)
return _E(self.scale / other, self.data / other)


def _getscaleoffset(expr):
a = expr(_Affine(1.0, 0.0))
return (a.m, a.b) if isinstance(a, _Affine) else (0.0, a)
a = expr(_E(1, 0))
return (a.scale, a.data) if isinstance(a, _E) else (0, a)


# --------------------------------------------------------------------
Expand Down

0 comments on commit 46802d5

Please sign in to comment.