Skip to content

Commit 46802d5

Browse files
radarherebenrg
authored andcommitted
Removed unused import and restored existing checks (#1)
* 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>
1 parent 4e12ccc commit 46802d5

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

Tests/test_image_point.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
from PIL import Image
4+
35
from .helper import assert_image_equal, hopper
46

57

@@ -17,13 +19,15 @@ def test_sanity():
1719
im.point(list(range(256)))
1820
im.point(lambda x: x * 1)
1921
im.point(lambda x: x + 1)
22+
im.point(lambda x: x - 1)
2023
im.point(lambda x: x * 1 + 1)
2124
im.point(lambda x: 0.1 + 0.2 * x)
2225
im.point(lambda x: -x)
2326
im.point(lambda x: x - 0.5)
2427
im.point(lambda x: 1 - x / 2)
2528
im.point(lambda x: (2 + x) / 3)
2629
im.point(lambda x: 0.5)
30+
im.point(lambda x: x / 1)
2731
with pytest.raises(TypeError):
2832
im.point(lambda x: x * x)
2933
with pytest.raises(TypeError):
@@ -55,3 +59,8 @@ def test_f_mode():
5559
im = hopper("F")
5660
with pytest.raises(ValueError):
5761
im.point(None)
62+
63+
64+
def test_coerce_e_deprecation():
65+
with pytest.warns(DeprecationWarning):
66+
assert Image.coerce_e(2).data == 2

src/PIL/Image.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import io
3030
import logging
3131
import math
32-
import numbers
3332
import os
3433
import re
3534
import struct
@@ -431,19 +430,23 @@ def _getencoder(mode, encoder_name, args, extra=()):
431430
# Simple expression analyzer
432431

433432

434-
# _Affine(m, b) represents the polynomial m x + b
435-
class _Affine:
436-
def __init__(self, m, b):
437-
self.m = m
438-
self.b = b
433+
def coerce_e(value):
434+
deprecate("coerce_e", 10)
435+
return value if isinstance(value, _E) else _E(1, value)
436+
437+
438+
class _E:
439+
def __init__(self, scale, data):
440+
self.scale = scale
441+
self.data = data
439442

440443
def __neg__(self):
441-
return _Affine(-self.m, -self.b)
444+
return _E(-self.scale, -self.data)
442445

443446
def __add__(self, other):
444-
if isinstance(other, _Affine):
445-
return _Affine(self.m + other.m, self.b + other.b)
446-
return _Affine(self.m, self.b + other)
447+
if isinstance(other, _E):
448+
return _E(self.scale + other.scale, self.data + other.data)
449+
return _E(self.scale, self.data + other)
447450

448451
__radd__ = __add__
449452

@@ -454,21 +457,21 @@ def __rsub__(self, other):
454457
return other + -self
455458

456459
def __mul__(self, other):
457-
if isinstance(other, _Affine):
460+
if isinstance(other, _E):
458461
return NotImplemented
459-
return _Affine(self.m * other, self.b * other)
462+
return _E(self.scale * other, self.data * other)
460463

461464
__rmul__ = __mul__
462465

463466
def __truediv__(self, other):
464-
if isinstance(other, _Affine):
467+
if isinstance(other, _E):
465468
return NotImplemented
466-
return _Affine(self.m / other, self.b / other)
469+
return _E(self.scale / other, self.data / other)
467470

468471

469472
def _getscaleoffset(expr):
470-
a = expr(_Affine(1.0, 0.0))
471-
return (a.m, a.b) if isinstance(a, _Affine) else (0.0, a)
473+
a = expr(_E(1, 0))
474+
return (a.scale, a.data) if isinstance(a, _E) else (0, a)
472475

473476

474477
# --------------------------------------------------------------------

0 commit comments

Comments
 (0)