From eb801d75f55c6fbea58809c78837f205f237481e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timothe=CC=81e=20Peignier?= Date: Fri, 27 Jan 2012 13:45:22 +0100 Subject: [PATCH] small pep8 formatting fix --- imagekit/processors/resize.py | 45 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/imagekit/processors/resize.py b/imagekit/processors/resize.py index 4e82bdf..f62dd2e 100644 --- a/imagekit/processors/resize.py +++ b/imagekit/processors/resize.py @@ -1,5 +1,5 @@ - import math + from imagekit.lib import Image @@ -61,14 +61,14 @@ class Crop(object): crop_x, crop_y = (abs(self.width - resize_x), abs(self.height - resize_y)) x_diff, y_diff = (int(crop_x / 2), int(crop_y / 2)) box_left, box_right = { - 0: (0, self.width), + 0: (0, self.width), 0.5: (int(x_diff), int(x_diff + self.width)), - 1: (int(crop_x), int(resize_x)), + 1: (int(crop_x), int(resize_x)), }[horizontal_anchor] box_upper, box_lower = { - 0: (0, self.height), + 0: (0, self.height), 0.5: (int(y_diff), int(y_diff + self.height)), - 1: (int(crop_y), int(resize_y)), + 1: (int(crop_y), int(resize_y)), }[vertical_anchor] box = (box_left, box_upper, box_right, box_lower) img = img.resize((int(resize_x), int(resize_y)), Image.ANTIALIAS).crop(box) @@ -117,61 +117,61 @@ def histogram_entropy(im): """ Calculate the entropy of an images' histogram. Used for "smart cropping" in easy-thumbnails; see: https://raw.github.com/SmileyChris/easy-thumbnails/master/easy_thumbnails/utils.py - + """ if not isinstance(im, Image.Image): - return 0 # Fall back to a constant entropy. - + return 0 # Fall back to a constant entropy. + histogram = im.histogram() hist_ceil = float(sum(histogram)) histonorm = [histocol / hist_ceil for histocol in histogram] - + return -sum([p * math.log(p, 2) for p in histonorm if p != 0]) class SmartCrop(object): """ Crop an image 'smartly' -- based on smart crop implementation from easy-thumbnails: - + https://github.com/SmileyChris/easy-thumbnails/blob/master/easy_thumbnails/processors.py#L193 - + Smart cropping whittles away the parts of the image with the least entropy. - + """ - + def __init__(self, width=None, height=None): self.width = width self.height = height - + def compare_entropy(self, start_slice, end_slice, slice, difference): """ Calculate the entropy of two slices (from the start and end of an axis), returning a tuple containing the amount that should be added to the start and removed from the end of the axis. - + """ start_entropy = histogram_entropy(start_slice) end_entropy = histogram_entropy(end_slice) - + if end_entropy and abs(start_entropy / end_entropy - 1) < 0.01: # Less than 1% difference, remove from both sides. if difference >= slice * 2: return slice, slice half_slice = slice // 2 return half_slice, slice - half_slice - + if start_entropy > end_entropy: return 0, slice else: return slice, 0 - + def process(self, img): source_x, source_y = img.size diff_x = int(source_x - min(source_x, self.width)) diff_y = int(source_y - min(source_y, self.height)) left = top = 0 right, bottom = source_x, source_y - + while diff_x: slice = min(diff_x, max(diff_x // 5, 10)) start = img.crop((left, 0, left + slice, source_y)) @@ -180,7 +180,7 @@ class SmartCrop(object): left += add right -= remove diff_x = diff_x - add - remove - + while diff_y: slice = min(diff_y, max(diff_y // 5, 10)) start = img.crop((0, top, source_x, top + slice)) @@ -189,9 +189,8 @@ class SmartCrop(object): top += add bottom -= remove diff_y = diff_y - add - remove - + box = (left, top, right, bottom) img = img.crop(box) - - return img + return img