From 0dfabd1d7bf8fa1f01cf8faa0bc1bc9b0c270311 Mon Sep 17 00:00:00 2001 From: Justin Driscoll Date: Fri, 9 Jan 2009 09:07:10 -0500 Subject: [PATCH] Renamed config_module to spec_module --- src/imagekit/models.py | 4 +- src/imagekit/options.py | 2 +- src/imagekit/processors.py | 130 ++++++++++++++++++------------------- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/imagekit/models.py b/src/imagekit/models.py index 9c5fc03..67531fb 100644 --- a/src/imagekit/models.py +++ b/src/imagekit/models.py @@ -20,10 +20,10 @@ class IKModelBase(ModelBase): user_opts = getattr(cls, 'IKConfig', None) opts = Options(user_opts) try: - module = __import__(opts.config_module, {}, {}, ['']) + module = __import__(opts.spec_module, {}, {}, ['']) except ImportError: raise ImportError('Unable to load imagekit config module: %s' % \ - opts.config_module) + opts.spec_module) for spec in [spec for spec in module.__dict__.values() \ if isinstance(spec, type) \ and issubclass(spec, specs.ImageSpec) \ diff --git a/src/imagekit/options.py b/src/imagekit/options.py index 8ddf89f..3c771fa 100644 --- a/src/imagekit/options.py +++ b/src/imagekit/options.py @@ -14,7 +14,7 @@ class Options(object): cache_dir = 'images' save_count_as = None cache_filename_format = "%(filename)s_%(specname)s.%(extension)s" - config_module = 'imagekit.config' + spec_module = 'imagekit.config' def __init__(self, opts): for key, value in opts.__dict__.iteritems(): diff --git a/src/imagekit/processors.py b/src/imagekit/processors.py index cf9f7eb..6f6b480 100644 --- a/src/imagekit/processors.py +++ b/src/imagekit/processors.py @@ -15,71 +15,6 @@ class ImageProcessor(object): return image -class Resize(ImageProcessor): - width = None - height = None - crop = False - upscale = False - - @classmethod - def process(cls, image, obj=None): - cur_width, cur_height = image.size - if cls.crop: - crop_horz = getattr(obj, obj._ik.crop_horz_field, 1) - crop_vert = getattr(obj, obj._ik.crop_vert_field, 1) - ratio = max(float(cls.width)/cur_width, float(cls.height)/cur_height) - resize_x, resize_y = ((cur_width * ratio), (cur_height * ratio)) - crop_x, crop_y = (abs(cls.width - resize_x), abs(cls.height - resize_y)) - x_diff, y_diff = (int(crop_x / 2), int(crop_y / 2)) - box_left, box_right = { - 0: (0, cls.width), - 1: (int(x_diff), int(x_diff + cls.width)), - 2: (int(crop_x), int(resize_x)), - }[crop_horz] - box_upper, box_lower = { - 0: (0, cls.height), - 1: (int(y_diff), int(y_diff + cls.height)), - 2: (int(crop_y), int(resize_y)), - }[crop_vert] - box = (box_left, box_upper, box_right, box_lower) - image = image.resize((int(resize_x), int(resize_y)), Image.ANTIALIAS).crop(box) - else: - if not cls.width is None and not cls.height is None: - ratio = min(float(cls.width)/cur_width, - float(cls.height)/cur_height) - else: - if cls.width is None: - ratio = float(cls.height)/cur_height - else: - ratio = float(cls.width)/cur_width - new_dimensions = (int(round(cur_width*ratio)), - int(round(cur_height*ratio))) - if new_dimensions[0] > cur_width or \ - new_dimensions[1] > cur_height: - if not cls.upscale: - return image - image = image.resize(new_dimensions, Image.ANTIALIAS) - return image - - -class Transpose(ImageProcessor): - """ Rotates or flips the image - - Method choices: - - FLIP_LEFT RIGHT - - FLIP_TOP_BOTTOM - - ROTATE_90 - - ROTATE_270 - - ROTATE_180 - - """ - method = 'FLIP_LEFT_RIGHT' - - @classmethod - def process(cls, image, obj=None): - return image.transpose(getattr(Image, cls.method)) - - class Adjustment(ImageProcessor): color = 1.0 brightness = 1.0 @@ -132,3 +67,68 @@ class Reflection(ImageProcessor): composite.paste(reflection, (0, image.size[1])) # return the image complete with reflection effect return composite + + +class Resize(ImageProcessor): + width = None + height = None + crop = False + upscale = False + + @classmethod + def process(cls, image, obj=None): + cur_width, cur_height = image.size + if cls.crop: + crop_horz = getattr(obj, obj._ik.crop_horz_field, 1) + crop_vert = getattr(obj, obj._ik.crop_vert_field, 1) + ratio = max(float(cls.width)/cur_width, float(cls.height)/cur_height) + resize_x, resize_y = ((cur_width * ratio), (cur_height * ratio)) + crop_x, crop_y = (abs(cls.width - resize_x), abs(cls.height - resize_y)) + x_diff, y_diff = (int(crop_x / 2), int(crop_y / 2)) + box_left, box_right = { + 0: (0, cls.width), + 1: (int(x_diff), int(x_diff + cls.width)), + 2: (int(crop_x), int(resize_x)), + }[crop_horz] + box_upper, box_lower = { + 0: (0, cls.height), + 1: (int(y_diff), int(y_diff + cls.height)), + 2: (int(crop_y), int(resize_y)), + }[crop_vert] + box = (box_left, box_upper, box_right, box_lower) + image = image.resize((int(resize_x), int(resize_y)), Image.ANTIALIAS).crop(box) + else: + if not cls.width is None and not cls.height is None: + ratio = min(float(cls.width)/cur_width, + float(cls.height)/cur_height) + else: + if cls.width is None: + ratio = float(cls.height)/cur_height + else: + ratio = float(cls.width)/cur_width + new_dimensions = (int(round(cur_width*ratio)), + int(round(cur_height*ratio))) + if new_dimensions[0] > cur_width or \ + new_dimensions[1] > cur_height: + if not cls.upscale: + return image + image = image.resize(new_dimensions, Image.ANTIALIAS) + return image + + +class Transpose(ImageProcessor): + """ Rotates or flips the image + + Method should be one of the following strings: + - FLIP_LEFT RIGHT + - FLIP_TOP_BOTTOM + - ROTATE_90 + - ROTATE_270 + - ROTATE_180 + + """ + method = 'FLIP_LEFT_RIGHT' + + @classmethod + def process(cls, image, obj=None): + return image.transpose(getattr(Image, cls.method))