def resize(self, img: WandImage, mult: float) -> WandImage: # No need to waste our resources for resizing by the same exact multiplier (1:1) if mult == 1.0: return img # Or even invalid multiplier (raise exception instead) elif mult <= 0: raise ValueError("Image resize multiplier must not be less than 0!") # Resize the image using the multiplier new_width = round(img.width * mult) new_height = round(img.height * mult) img.adaptive_resize(new_width, new_height) # Then crop the image so it's centered crop_left = round((self.max_width - img.width) / 2) if crop_left < 0: crop_left = 0 crop_right = crop_left + self.max_width if crop_right > img.width: crop_right = img.width img.crop(crop_left, 0, crop_right, self.max_height) return img