Skip to content

Commit 83e46c0

Browse files
committed
Add support for Pillow 7.1.0
1 parent befc950 commit 83e46c0

File tree

5 files changed

+76
-10
lines changed

5 files changed

+76
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ class MyModel(models.Model):
5959

6060
# is the same as dictionary-style call
6161
image = StdImageField(upload_to='path/to/img', variations={'thumbnail': (100, 75)})
62-
62+
6363
# variations are converted to JPEGs
6464
jpeg = JPEGField(
6565
upload_to='path/to/img',
66-
variations={'full': (float('inf'), float('inf')), 'thumbnail': (100, 75)},
66+
variations={'full': (None, None), 'thumbnail': (100, 75)},
6767
)
6868

6969
# creates a thumbnail resized to 100x100 croping if necessary

stdimage/models.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def process_variation(cls, variation, image):
102102
)
103103

104104
size = variation['width'], variation['height']
105-
size = tuple(int(i) if i != float('inf') else i
105+
size = tuple(int(i) if i is not None else i
106106
for i in size)
107107

108108
if file_format == 'JPEG':
@@ -168,8 +168,8 @@ class StdImageField(ImageField):
168168
descriptor_class = StdImageFileDescriptor
169169
attr_class = StdImageFieldFile
170170
def_variation = {
171-
'width': float('inf'),
172-
'height': float('inf'),
171+
'width': None,
172+
'height': None,
173173
'crop': False,
174174
'resample': Image.ANTIALIAS,
175175
}
@@ -308,6 +308,12 @@ def process_variation(cls, variation, image):
308308

309309
resample = variation['resample']
310310

311+
if variation['width'] is None:
312+
variation['width'] = image.size[0]
313+
314+
if variation['height'] is None:
315+
variation['height'] = image.size[1]
316+
311317
factor = 1
312318
while image.size[0] / factor \
313319
> 2 * variation['width'] \
@@ -322,7 +328,7 @@ def process_variation(cls, variation, image):
322328
)
323329

324330
size = variation['width'], variation['height']
325-
size = tuple(int(i) if i != float('inf') else i
331+
size = tuple(int(i) if i is not None else i
326332
for i in size)
327333

328334
# http://stackoverflow.com/a/21669827

stdimage/validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def compare(self, x):
1313
return True
1414

1515
def __init__(self, width, height):
16-
self.limit_value = width, height
16+
self.limit_value = width or float('inf'), height or float('inf')
1717

1818
def __call__(self, value):
1919
cleaned = self.clean(value)
@@ -36,7 +36,7 @@ class MaxSizeValidator(BaseSizeValidator):
3636
"""
3737
ImageField validator to validate the max width and height of an image.
3838
39-
You may use float("inf") as an infinite boundary.
39+
You may use None as an infinite boundary.
4040
"""
4141

4242
def compare(self, img_size, max_size):
@@ -51,7 +51,7 @@ class MinSizeValidator(BaseSizeValidator):
5151
"""
5252
ImageField validator to validate the min width and height of an image.
5353
54-
You may use float("inf") as an infinite boundary.
54+
You may use None as an infinite boundary.
5555
"""
5656

5757
def compare(self, img_size, min_size):

tests/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class JPEGModel(models.Model):
6565
upload_to=upload_to,
6666
blank=True,
6767
variations={
68-
'full': (float('inf'), float('inf')),
68+
'full': (None, None),
6969
'thumbnail': (100, 75, True),
7070
},
7171
delete_orphans=True,

tests/test_validators.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from stdimage import validators
2+
3+
4+
class TestBaseSizeValidator:
5+
def test_init__none(self):
6+
assert validators.MinSizeValidator(None, None).limit_value == (
7+
float('inf'), float('inf')
8+
)
9+
10+
11+
class TestMaxSizeValidator:
12+
def test_compare__inf(self):
13+
limit_value = float("inf"), float("inf")
14+
instance = validators.MaxSizeValidator(*limit_value)
15+
assert not instance.compare((300, 200), limit_value)
16+
17+
def test_compare__eq(self):
18+
assert not validators.MaxSizeValidator(300, 200).compare((300, 200), (300, 200))
19+
20+
def test_compare__gt(self):
21+
limit_value = 300, 200
22+
instance = validators.MaxSizeValidator(*limit_value)
23+
assert instance.compare((600, 400), limit_value)
24+
assert instance.compare((600, 200), limit_value)
25+
assert instance.compare((300, 400), limit_value)
26+
assert instance.compare((600, 100), limit_value)
27+
assert instance.compare((150, 400), limit_value)
28+
29+
def test_compare__lt(self):
30+
limit_value = 300, 200
31+
instance = validators.MaxSizeValidator(*limit_value)
32+
assert not instance.compare((150, 100), (300, 200))
33+
assert not instance.compare((300, 100), (300, 200))
34+
assert not instance.compare((150, 200), (300, 200))
35+
36+
37+
class TestMinSizeValidator:
38+
def test_compare__inf(self):
39+
limit_value = float("inf"), float("inf")
40+
instance = validators.MinSizeValidator(*limit_value)
41+
assert instance.compare((300, 200), limit_value)
42+
43+
def test_compare__eq(self):
44+
assert not validators.MinSizeValidator(300, 200).compare((300, 200), (300, 200))
45+
46+
def test_compare__gt(self):
47+
limit_value = 300, 200
48+
instance = validators.MinSizeValidator(*limit_value)
49+
assert not instance.compare((600, 400), limit_value)
50+
assert not instance.compare((600, 200), limit_value)
51+
assert not instance.compare((300, 400), limit_value)
52+
assert instance.compare((600, 100), limit_value)
53+
assert instance.compare((150, 400), limit_value)
54+
55+
def test_compare__lt(self):
56+
limit_value = 300, 200
57+
instance = validators.MinSizeValidator(*limit_value)
58+
assert instance.compare((150, 100), (300, 200))
59+
assert instance.compare((300, 100), (300, 200))
60+
assert instance.compare((150, 200), (300, 200))

0 commit comments

Comments
 (0)