Skip to content

Commit

Permalink
Add support for Pillow 7.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Apr 2, 2020
1 parent befc950 commit 83e46c0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class MyModel(models.Model):

# is the same as dictionary-style call
image = StdImageField(upload_to='path/to/img', variations={'thumbnail': (100, 75)})

# variations are converted to JPEGs
jpeg = JPEGField(
upload_to='path/to/img',
variations={'full': (float('inf'), float('inf')), 'thumbnail': (100, 75)},
variations={'full': (None, None), 'thumbnail': (100, 75)},
)

# creates a thumbnail resized to 100x100 croping if necessary
Expand Down
14 changes: 10 additions & 4 deletions stdimage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def process_variation(cls, variation, image):
)

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

if file_format == 'JPEG':
Expand Down Expand Up @@ -168,8 +168,8 @@ class StdImageField(ImageField):
descriptor_class = StdImageFileDescriptor
attr_class = StdImageFieldFile
def_variation = {
'width': float('inf'),
'height': float('inf'),
'width': None,
'height': None,
'crop': False,
'resample': Image.ANTIALIAS,
}
Expand Down Expand Up @@ -308,6 +308,12 @@ def process_variation(cls, variation, image):

resample = variation['resample']

if variation['width'] is None:
variation['width'] = image.size[0]

if variation['height'] is None:
variation['height'] = image.size[1]

factor = 1
while image.size[0] / factor \
> 2 * variation['width'] \
Expand All @@ -322,7 +328,7 @@ def process_variation(cls, variation, image):
)

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

# http://stackoverflow.com/a/21669827
Expand Down
6 changes: 3 additions & 3 deletions stdimage/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def compare(self, x):
return True

def __init__(self, width, height):
self.limit_value = width, height
self.limit_value = width or float('inf'), height or float('inf')

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

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

def compare(self, img_size, min_size):
Expand Down
2 changes: 1 addition & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class JPEGModel(models.Model):
upload_to=upload_to,
blank=True,
variations={
'full': (float('inf'), float('inf')),
'full': (None, None),
'thumbnail': (100, 75, True),
},
delete_orphans=True,
Expand Down
60 changes: 60 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from stdimage import validators


class TestBaseSizeValidator:
def test_init__none(self):
assert validators.MinSizeValidator(None, None).limit_value == (
float('inf'), float('inf')
)


class TestMaxSizeValidator:
def test_compare__inf(self):
limit_value = float("inf"), float("inf")
instance = validators.MaxSizeValidator(*limit_value)
assert not instance.compare((300, 200), limit_value)

def test_compare__eq(self):
assert not validators.MaxSizeValidator(300, 200).compare((300, 200), (300, 200))

def test_compare__gt(self):
limit_value = 300, 200
instance = validators.MaxSizeValidator(*limit_value)
assert instance.compare((600, 400), limit_value)
assert instance.compare((600, 200), limit_value)
assert instance.compare((300, 400), limit_value)
assert instance.compare((600, 100), limit_value)
assert instance.compare((150, 400), limit_value)

def test_compare__lt(self):
limit_value = 300, 200
instance = validators.MaxSizeValidator(*limit_value)
assert not instance.compare((150, 100), (300, 200))
assert not instance.compare((300, 100), (300, 200))
assert not instance.compare((150, 200), (300, 200))


class TestMinSizeValidator:
def test_compare__inf(self):
limit_value = float("inf"), float("inf")
instance = validators.MinSizeValidator(*limit_value)
assert instance.compare((300, 200), limit_value)

def test_compare__eq(self):
assert not validators.MinSizeValidator(300, 200).compare((300, 200), (300, 200))

def test_compare__gt(self):
limit_value = 300, 200
instance = validators.MinSizeValidator(*limit_value)
assert not instance.compare((600, 400), limit_value)
assert not instance.compare((600, 200), limit_value)
assert not instance.compare((300, 400), limit_value)
assert instance.compare((600, 100), limit_value)
assert instance.compare((150, 400), limit_value)

def test_compare__lt(self):
limit_value = 300, 200
instance = validators.MinSizeValidator(*limit_value)
assert instance.compare((150, 100), (300, 200))
assert instance.compare((300, 100), (300, 200))
assert instance.compare((150, 200), (300, 200))

0 comments on commit 83e46c0

Please sign in to comment.