Skip to content

Commit ab24c42

Browse files
authored
Fixed Pillow AttributeError and fixed generated base64 images (#36)
* 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 * 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. * Updated test case * Fixed version comparison
1 parent 329ffcd commit ab24c42

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

jsignature/utils.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
import json
66
from itertools import chain
7-
from PIL import Image, ImageDraw, ImageOps
7+
from PIL import Image, ImageDraw, ImageOps, __version__ as PIL_VERSION
88

99
AA = 5 # super sampling gor antialiasing
1010

@@ -29,24 +29,37 @@ def _remove_empty_pts(pt):
2929
raise ValueError
3030

3131
# Compute box
32-
width = int(round(max(chain(*[d['x'] for d in drawing])))) + 10
33-
height = int(round(max(chain(*[d['y'] for d in drawing])))) + 10
32+
min_width = int(round(min(chain(*[d['x'] for d in drawing])))) - 10
33+
max_width = int(round(max(chain(*[d['x'] for d in drawing])))) + 10
34+
width = max_width - min_width
35+
min_height = int(round(min(chain(*[d['y'] for d in drawing])))) - 10
36+
max_height = int(round(max(chain(*[d['y'] for d in drawing])))) + 10
37+
height = max_height - min_height
3438

3539
# Draw image
3640
im = Image.new("RGBA", (width * AA, height * AA))
3741
draw = ImageDraw.Draw(im)
3842
for line in drawing:
3943
len_line = len(line['x'])
40-
points = [(line['x'][i] * AA, line['y'][i] * AA)
41-
for i in range(0, len_line)]
44+
points = [
45+
(
46+
(line['x'][i] - min_width) * AA,
47+
(line['y'][i] - min_height) * AA
48+
)
49+
for i in range(0, len_line)
50+
]
4251
draw.line(points, fill="#000", width=2 * AA)
4352
im = ImageOps.expand(im)
4453
# Smart crop
4554
bbox = im.getbbox()
4655
if bbox:
4756
im.crop(bbox)
4857

49-
im.thumbnail((width, height), Image.ANTIALIAS)
58+
old_pil_version = int(PIL_VERSION.split('.')[0]) < 10
59+
im.thumbnail(
60+
(width, height),
61+
Image.ANTIALIAS if old_pil_version else Image.LANCZOS
62+
)
5063

5164
if as_file:
5265
ret = im._dump(format='PNG')

tests/test_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ def test_inputs_bad_type_value(self):
1616

1717
def test_outputs_as_base64(self):
1818
output = signature_base64(DUMMY_STR_VALUE)
19-
self.assertEqual(output, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANwAAABNCAYAAADNYApnAAABIklEQVR4nO3ZP0odURjG4d/ce5dgE3EHgXTp07iD6B7cga3YpUoh7iFrCti4BkGw0OLMoEVIEW4yxPs81ZxzGPial+/8KQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPh3ttW0dhFwCN4GbbNaFXAAlrCdVh/m791KtcC7tmkE7mP1XP2sPs1rQgd7NjVCd1TdNkL3UJ2tWRQciovqqRG8q0aXc6aDPZsat5RVX6q7Rui+zXPbX/wD/KFla7mc306qH9XneazLwZ4s3eu8eqwuV6wF3rXllvK4um9sI7/Oc7s8hMNeLVf/3xthu5nHzmzwF0y9drjrxvPAMgcA/7/lWUBnAwAAAAAAfucFwt4TZmdXW+AAAAAASUVORK5CYII=")
19+
self.assertEqual(output, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAcCAYAAACUJBTQAAAAuElEQVR4nO3TMQ4BQRSH8d8uR9AQN5Do9Bo3wB3cQCs6lULcwZkkGmeQSBQU+yZEVIxC7FfNvGTnm7fvP9TU/A0NFN8UPB5eflMwQjvWzZyCMiQ9XLFHP7eoCFEL2xCdMMkleMUMl5AtVN1km1GhShcMcQjRKmqNF9+8JSnd59HFDoPYf9xNuuUUZ8w/PfCZlK4OjqpfNI5aU6bHmWK6DsEm9llmkCjcO1mqopxqv0mK8O92UFPzPjdRMBNmBFDqcwAAAABJRU5ErkJggg==")

0 commit comments

Comments
 (0)