2009-01-04 18:30:03 +00:00
|
|
|
""" ImageKit image specifications
|
2009-01-04 17:38:06 +00:00
|
|
|
|
2009-01-04 18:30:03 +00:00
|
|
|
All imagekit specifications must inherit from the ImageSpec class. Models
|
|
|
|
|
inheriting from IKModel will be modified with a descriptor/accessor for each
|
|
|
|
|
spec found.
|
2009-01-04 17:38:06 +00:00
|
|
|
|
|
|
|
|
"""
|
2009-01-04 22:14:13 +00:00
|
|
|
import os
|
|
|
|
|
from imagekit import Image
|
|
|
|
|
|
2009-01-04 18:30:03 +00:00
|
|
|
class ImageSpec(object):
|
2009-01-04 22:14:13 +00:00
|
|
|
pre_cache = False
|
2009-01-04 18:30:03 +00:00
|
|
|
output_quality = 70
|
2009-01-04 17:38:06 +00:00
|
|
|
increment_count = False
|
|
|
|
|
processors = []
|
2009-01-04 18:30:03 +00:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def name(cls):
|
|
|
|
|
return getattr(cls, 'access_as', cls.__name__.lower())
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2009-01-04 18:40:22 +00:00
|
|
|
def process(cls, image, save_as=None):
|
2009-01-04 18:30:03 +00:00
|
|
|
processed_image = image.copy()
|
|
|
|
|
for proc in cls.processors:
|
|
|
|
|
processed_image = proc.process(processed_image)
|
2009-01-04 18:40:22 +00:00
|
|
|
|
|
|
|
|
if save_as is not None:
|
|
|
|
|
try:
|
|
|
|
|
if image.format != 'JPEG':
|
|
|
|
|
try:
|
|
|
|
|
processed_image.save(save_as)
|
|
|
|
|
return
|
|
|
|
|
except KeyError:
|
|
|
|
|
pass
|
|
|
|
|
processed_image.save(save_as, 'JPEG',
|
|
|
|
|
quality=int(cls.output_quality),
|
|
|
|
|
optimize=True)
|
|
|
|
|
except IOError, e:
|
2009-01-04 22:14:13 +00:00
|
|
|
if os.path.isfile(save_as):
|
|
|
|
|
os.remove(save_as)
|
2009-01-04 18:40:22 +00:00
|
|
|
raise e
|
|
|
|
|
|
2009-01-04 18:30:03 +00:00
|
|
|
return processed_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Accessor(object):
|
|
|
|
|
def __init__(self, obj, spec):
|
|
|
|
|
self._img = None
|
|
|
|
|
self._obj = obj
|
|
|
|
|
self.spec = spec
|
|
|
|
|
|
|
|
|
|
def create(self):
|
2009-01-04 22:14:13 +00:00
|
|
|
if not os.path.isdir(self._obj.cache_dir):
|
|
|
|
|
os.makedirs(self._obj.cache_dir)
|
2009-01-06 19:00:53 +00:00
|
|
|
self._img = self.spec.process(Image.open(self._obj.ik_image_field.path), save_as=self.path)
|
2009-01-04 18:30:03 +00:00
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
|
if self.exists:
|
|
|
|
|
os.remove(self.path)
|
|
|
|
|
self._img = None
|
|
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
@property
|
|
|
|
|
def exists(self):
|
|
|
|
|
return os.path.isfile(self.path)
|
|
|
|
|
|
2009-01-04 18:30:03 +00:00
|
|
|
@property
|
|
|
|
|
def name(self):
|
2009-01-06 19:00:53 +00:00
|
|
|
filename, ext = os.path.splitext(os.path.basename(self._obj.ik_image_field.path))
|
2009-01-04 22:14:13 +00:00
|
|
|
return self._obj._ik.cache_filename_format % \
|
2009-01-04 18:30:03 +00:00
|
|
|
{'filename': filename,
|
2009-01-04 22:14:13 +00:00
|
|
|
'specname': self.spec.name(),
|
2009-01-04 18:30:03 +00:00
|
|
|
'extension': ext.lstrip('.')}
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
2009-01-04 22:14:13 +00:00
|
|
|
return os.path.abspath(os.path.join(self._obj.cache_dir, self.name))
|
2009-01-04 18:30:03 +00:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def url(self):
|
|
|
|
|
if not self.exists:
|
|
|
|
|
self.create()
|
2009-01-06 19:00:53 +00:00
|
|
|
if self.spec.increment_count:
|
2009-01-04 18:30:03 +00:00
|
|
|
fieldname = self._obj._ik.save_count_as
|
|
|
|
|
if fieldname is not None:
|
|
|
|
|
current_count = getattr(self._obj, fieldname)
|
|
|
|
|
setattr(self._obj, fieldname, current_count + 1)
|
|
|
|
|
return '/'.join([self._obj.cache_url, self.name])
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def image(self):
|
|
|
|
|
if self._img is None:
|
2009-01-04 22:14:13 +00:00
|
|
|
if not self.exists:
|
2009-01-04 18:30:03 +00:00
|
|
|
self.create()
|
|
|
|
|
else:
|
|
|
|
|
self._img = Image.open(self.path)
|
|
|
|
|
return self._img
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def width(self):
|
|
|
|
|
return self.image.size[0]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def height(self):
|
|
|
|
|
return self.image.size[1]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def file(self):
|
|
|
|
|
if not self.exists:
|
|
|
|
|
self.create()
|
|
|
|
|
return open(self.path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Descriptor(object):
|
|
|
|
|
def __init__(self, spec):
|
|
|
|
|
self._spec = spec
|
|
|
|
|
|
|
|
|
|
def __get__(self, obj, type=None):
|
2009-01-06 19:00:53 +00:00
|
|
|
return Accessor(obj, self._spec)
|