Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(Dimensions(160, 320), 0.5),
])
def test_dimensions_aspect_ratio(dim, ar):
assert dim.aspect_ratio == ar
def test_dimensions_stretch(source, target, upscale, rescale):
# We don't take a crop parameter here since it will always be the same as
# rescale
source = Dimensions(*source)
target_width, target_height = target
rescale = Dimensions(*rescale)
new_dims = source.stretch(target_width, target_height, upscale)
assert rescale == new_dims.rescale
assert rescale == new_dims.crop
def _infer_dimensions(self, width, height):
"""Calculate dimensions based on aspect ratio if height, width or both
are missing.
"""
if width is None and height is None:
return self
if width is None:
width = _imround(height * self.aspect_ratio)
elif height is None:
height = _imround(width / self.aspect_ratio)
return Dimensions(width, height)
:param min_height: Minimum height for the new rescaled dimensions.
:param upscale: Allow making the dimensions larger (default True).
:return: Rescaling operations
:rtype: Rescaling
"""
if upscale is None:
upscale = True
min_dim = self._infer_dimensions(min_width, min_height)
# Check if we should rescale at all
if min_dim == self or (not upscale and min_dim.contains(self)):
return Rescaling(self, self)
ar = self.aspect_ratio
rescale_dim = Dimensions(
width=_imround(max(min_dim.width, min_dim.height * ar)),
height=_imround(max(min_dim.height, min_dim.width / ar)),
)
return Rescaling(rescale=rescale_dim, crop=min_dim)
:param max_height: Maximum height for the new rescaled dimensions.
:param upscale: Allow making the dimensions larger (default False).
:return: Rescaling operations
:rtype: Rescaling
"""
if upscale is None:
upscale = False
max_dim = self._infer_dimensions(max_width, max_height)
# Check if we should rescale at all
if max_dim == self or (not upscale and max_dim.contains(self)):
return Rescaling(self, self)
ar = self.aspect_ratio
rescale_dim = Dimensions(
width=_imround(min(max_dim.width, max_dim.height * ar)),
height=_imround(min(max_dim.height, max_dim.width / ar)),
)
return Rescaling(rescale=rescale_dim, crop=rescale_dim)