From d6e57e4490e802e1ef500b68582d09291705e88f Mon Sep 17 00:00:00 2001 From: David Jean Louis Date: Tue, 7 Nov 2023 16:54:15 +0100 Subject: [PATCH 1/4] Fixed AttributeError with newer Pillow versions Image.ANTIALIAS constant was removed in PIL 10.0.0. This commit ensures that the new Image.LANCZOS is used for newer PIL versions. See: https://pillow.readthedocs.io/en/stable/releasenotes/10.0.0.html#constants --- jsignature/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/jsignature/utils.py b/jsignature/utils.py index eb3dc9a..ad71a43 100644 --- a/jsignature/utils.py +++ b/jsignature/utils.py @@ -4,7 +4,7 @@ """ import json from itertools import chain -from PIL import Image, ImageDraw, ImageOps +from PIL import Image, ImageDraw, ImageOps, __version__ as PIL_VERSION AA = 5 # super sampling gor antialiasing @@ -46,7 +46,10 @@ def _remove_empty_pts(pt): if bbox: im.crop(bbox) - im.thumbnail((width, height), Image.ANTIALIAS) + im.thumbnail( + (width, height), + Image.ANTIALIAS if PIL_VERSION < '10.0.0' else Image.LANCZOS + ) if as_file: ret = im._dump(format='PNG') From 1d0c1fe304b8e3fc338cfd1bd75b6480cd740e07 Mon Sep 17 00:00:00 2001 From: David Jean Louis Date: Tue, 7 Nov 2023 16:57:47 +0100 Subject: [PATCH 2/4] Fixed dimensions / cropping of base64 images Previously, generated images where cropped to the right but not to the left, resulting in right centered images. This commit makes generated images cropped from center. --- jsignature/utils.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/jsignature/utils.py b/jsignature/utils.py index ad71a43..64807e2 100644 --- a/jsignature/utils.py +++ b/jsignature/utils.py @@ -29,16 +29,25 @@ def _remove_empty_pts(pt): raise ValueError # Compute box - width = int(round(max(chain(*[d['x'] for d in drawing])))) + 10 - height = int(round(max(chain(*[d['y'] for d in drawing])))) + 10 + min_width = int(round(min(chain(*[d['x'] for d in drawing])))) - 10 + max_width = int(round(max(chain(*[d['x'] for d in drawing])))) + 10 + width = max_width - min_width + min_height = int(round(min(chain(*[d['y'] for d in drawing])))) - 10 + max_height = int(round(max(chain(*[d['y'] for d in drawing])))) + 10 + height = max_height - min_height # Draw image im = Image.new("RGBA", (width * AA, height * AA)) draw = ImageDraw.Draw(im) for line in drawing: len_line = len(line['x']) - points = [(line['x'][i] * AA, line['y'][i] * AA) - for i in range(0, len_line)] + points = [ + ( + (line['x'][i] - min_width) * AA, + (line['y'][i] - min_height) * AA + ) + for i in range(0, len_line) + ] draw.line(points, fill="#000", width=2 * AA) im = ImageOps.expand(im) # Smart crop From 978fa5bd0c80c696ef0df8f7031ba1164d452a09 Mon Sep 17 00:00:00 2001 From: David Jean Louis Date: Tue, 7 Nov 2023 17:15:09 +0100 Subject: [PATCH 3/4] Updated test case --- tests/test_filter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_filter.py b/tests/test_filter.py index e79800b..ce9bca9 100644 --- a/tests/test_filter.py +++ b/tests/test_filter.py @@ -16,4 +16,4 @@ def test_inputs_bad_type_value(self): def test_outputs_as_base64(self): output = signature_base64(DUMMY_STR_VALUE) - self.assertEqual(output, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANwAAABNCAYAAADNYApnAAABIklEQVR4nO3ZP0odURjG4d/ce5dgE3EHgXTp07iD6B7cga3YpUoh7iFrCti4BkGw0OLMoEVIEW4yxPs81ZxzGPial+/8KQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPh3ttW0dhFwCN4GbbNaFXAAlrCdVh/m791KtcC7tmkE7mP1XP2sPs1rQgd7NjVCd1TdNkL3UJ2tWRQciovqqRG8q0aXc6aDPZsat5RVX6q7Rui+zXPbX/wD/KFla7mc306qH9XneazLwZ4s3eu8eqwuV6wF3rXllvK4um9sI7/Oc7s8hMNeLVf/3xthu5nHzmzwF0y9drjrxvPAMgcA/7/lWUBnAwAAAAAAfucFwt4TZmdXW+AAAAAASUVORK5CYII=") + self.assertEqual(output, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAcCAYAAACUJBTQAAAAuElEQVR4nO3TMQ4BQRSH8d8uR9AQN5Do9Bo3wB3cQCs6lULcwZkkGmeQSBQU+yZEVIxC7FfNvGTnm7fvP9TU/A0NFN8UPB5eflMwQjvWzZyCMiQ9XLFHP7eoCFEL2xCdMMkleMUMl5AtVN1km1GhShcMcQjRKmqNF9+8JSnd59HFDoPYf9xNuuUUZ8w/PfCZlK4OjqpfNI5aU6bHmWK6DsEm9llmkCjcO1mqopxqv0mK8O92UFPzPjdRMBNmBFDqcwAAAABJRU5ErkJggg==") From fd74a5a976843dede839a24986c581a3500b63c3 Mon Sep 17 00:00:00 2001 From: David Jean Louis Date: Tue, 7 Nov 2023 17:27:44 +0100 Subject: [PATCH 4/4] Fixed version comparison --- jsignature/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jsignature/utils.py b/jsignature/utils.py index 64807e2..c59cf72 100644 --- a/jsignature/utils.py +++ b/jsignature/utils.py @@ -55,9 +55,10 @@ def _remove_empty_pts(pt): if bbox: im.crop(bbox) + old_pil_version = int(PIL_VERSION.split('.')[0]) < 10 im.thumbnail( (width, height), - Image.ANTIALIAS if PIL_VERSION < '10.0.0' else Image.LANCZOS + Image.ANTIALIAS if old_pil_version else Image.LANCZOS ) if as_file: