From 46802d5def59b6694e5243e428b6419dc8a5ab43 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Tue, 3 May 2022 09:01:23 +1000 Subject: [PATCH] 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 --- Tests/test_image_point.py | 9 +++++++++ src/PIL/Image.py | 35 +++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Tests/test_image_point.py b/Tests/test_image_point.py index 2a4218bf8..140b7a3c9 100644 --- a/Tests/test_image_point.py +++ b/Tests/test_image_point.py @@ -1,5 +1,7 @@ import pytest +from PIL import Image + from .helper import assert_image_equal, hopper @@ -17,6 +19,7 @@ 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) @@ -24,6 +27,7 @@ def test_sanity(): 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): @@ -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 diff --git a/src/PIL/Image.py b/src/PIL/Image.py index e3a1eac70..114f4adb3 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -29,7 +29,6 @@ import builtins import io import logging import math -import numbers import os import re import struct @@ -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__ @@ -454,21 +457,21 @@ class _Affine: 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) # --------------------------------------------------------------------