def scale(self, image, geometry, upscale, crop): """ Given an image, scales the image down (or up, if ``upscale`` equates to a boolean ``True``). :param Image image: This is your engine's ``Image`` object. For PIL it's PIL.Image. :param tuple geometry: Geometry of the image in the format of (x,y). :returns: The scaled image. The returned type depends on your choice of Engine. """ x_image, y_image = map(float, self.get_image_size(image)) # Mod by ksperkins, 2-10-2012 # Allow only x or y geometry and scale other to maintain aspect # Unspecified value indicated by 0 -- if both 0 return original # If specified value in geometry is larger than image value and # upscale not specified then return original. # It's irrational to use this feature and have crop be set, thus # crop is temporarily repurposed to support upscaling with less code if geometry[0] <= 0 or geometry[1] <= 0: if geometry[0] <= 0 and geometry[1] <= 0: return image elif geometry[0] <= 0: if geometry[1] > y_image: # Image potentially needs upscaling, however the smaller # factor will just be 1, so we need to force selecting # the larger factor if upscaling is True. The following # line does just that. crop = upscale geometry = (x_image, geometry[1]) else: if geometry[0] > x_image: crop = upscale # See comment above geometry = (geometry[0], y_image) # End Mod # Calculate scaling factor. factors = (geometry[0] / x_image, geometry[1] / y_image) factor = max(factors) if crop else min(factors) if factor < 1 or upscale: width = toint(x_image * factor) height = toint(y_image * factor) image = self._scale(image, width, height) return image
def scale(self, image, geometry, upscale, crop): """ Given an image, scales the image down (or up, if ``upscale`` equates to a boolean ``True``). :param Image image: This is your engine's ``Image`` object. For PIL it's PIL.Image. :param tuple geometry: Geometry of the image in the format of (x,y). :returns: The scaled image. The returned type depends on your choice of Engine. """ x_image, y_image = map(float, self.get_image_size(image)) # Calculate scaling factor. factors = (geometry[0] / x_image, geometry[1] / y_image) factor = max(factors) if crop else min(factors) if factor < 1 or upscale: width = toint(x_image * factor) height = toint(y_image * factor) image = self._scale(image, width, height) return image