mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-04-25 23:44:45 +00:00
Added processors module
This commit is contained in:
parent
9242b3fd25
commit
72310161d1
4 changed files with 88 additions and 2 deletions
14
src/imagekit/ikconfig.py
Normal file
14
src/imagekit/ikconfig.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from imagekit import specs
|
||||
|
||||
|
||||
class ResizeThumbnail(specs.Resize):
|
||||
width = 100
|
||||
height = 75
|
||||
crop = True
|
||||
|
||||
class EnhanceSmall(specs.Adjustment):
|
||||
contrast = 1.2
|
||||
sharpness = 1.1
|
||||
|
||||
class Thumbnail(specs.Spec):
|
||||
processors = [ResizeThumbnail, EnhanceSmall]
|
||||
|
|
@ -14,7 +14,7 @@ class IKProperty(object):
|
|||
self.spec = spec
|
||||
|
||||
def create(self):
|
||||
resize_to_spec(self.image, self.spec)
|
||||
self.spec.process(self.image, self.path)
|
||||
|
||||
def delete(self):
|
||||
if self.exists:
|
||||
|
|
@ -62,7 +62,7 @@ class IKProperty(object):
|
|||
@property
|
||||
def file(self):
|
||||
if not self.exists:
|
||||
return None
|
||||
self.create()
|
||||
return open(self.path)
|
||||
|
||||
|
||||
|
|
|
|||
60
src/imagekit/processors.py
Normal file
60
src/imagekit/processors.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
""" Imagekit Image "ImageProcessors"
|
||||
|
||||
A processor defines a set of class variables (optional) and a
|
||||
class method named "process" which processes the supplied image using
|
||||
the class properties as settings. The process method can be overridden as well allowing user to define their
|
||||
own effects/processes entirely.
|
||||
|
||||
"""
|
||||
|
||||
class ImageProcessor(object):
|
||||
""" Base image processor class """
|
||||
@classmethod
|
||||
def process(cls, image):
|
||||
return image
|
||||
|
||||
|
||||
class Resize(ImageProcessor):
|
||||
width = None
|
||||
height = None
|
||||
crop = False
|
||||
upscale = False
|
||||
|
||||
|
||||
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):
|
||||
return image.transpose(getattr(Image, cls.method))
|
||||
|
||||
|
||||
class Adjustment(ImageProcessor):
|
||||
color = 1.0
|
||||
brightness = 1.0
|
||||
contrast = 1.0
|
||||
sharpness = 1.0
|
||||
|
||||
@classmethod
|
||||
def process(cls, image):
|
||||
for name in ['Color', 'Brightness', 'Contrast', 'Sharpness']:
|
||||
factor = getattr(cls, name.lower())
|
||||
if factor != 1.0:
|
||||
image = getattr(ImageEnhance, name)(image).enhance(factor)
|
||||
return image
|
||||
|
||||
|
||||
class Reflection(ImageProcessor):
|
||||
background_color = '#fffff'
|
||||
size = 0.0
|
||||
opacity = 0.6
|
||||
12
src/imagekit/specs.py
Normal file
12
src/imagekit/specs.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
""" Base imagekit specification classes
|
||||
|
||||
This module holds the base implementations and defaults for imagekit
|
||||
specification classes. Users import and subclass these classes to define new
|
||||
specifications.
|
||||
|
||||
"""
|
||||
class Spec(object):
|
||||
increment_count = False
|
||||
pre_cache = False
|
||||
jpeg_quality = 70
|
||||
processors = []
|
||||
Loading…
Reference in a new issue