Spec.process now only optionally saves the new image to disk

This commit is contained in:
Justin Driscoll 2009-01-04 13:40:22 -05:00
parent 9662ec2be0
commit 390e60c558
4 changed files with 32 additions and 17 deletions

View file

@ -0,0 +1,10 @@
"""
Django ImageKit
Author: Justin Driscoll <justin.driscoll@gmail.com>
Version: 1.0
"""
from models import IKModel
from specs import ImageSpec

View file

@ -4,6 +4,7 @@ from django.conf import settings
from django.db import models
from django.db.models.base import ModelBase
from django.utils.translation import ugettext_lazy as _
from imagekit.options import Options
from imagekit import specs

View file

@ -17,24 +17,27 @@ class ImageSpec(object):
return getattr(cls, 'access_as', cls.__name__.lower())
@classmethod
def create(cls, image, filename):
def process(cls, image, save_as=None):
processed_image = image.copy()
for proc in cls.processors:
processed_image = proc.process(processed_image)
try:
if image.format != 'JPEG':
try:
processed_image.save(im_filename)
return
except KeyError:
pass
processed_image.save(filename, 'JPEG',
quality=int(cls.output_quality),
optimize=True)
except IOError, e:
if os.path.isfile(filename):
os.unlink(filename)
raise e
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:
if os.path.isfile(filename):
os.unlink(filename)
raise e
return processed_image
@ -45,7 +48,7 @@ class Accessor(object):
self.spec = spec
def create(self):
self._img = self.spec.create(self.image, self.path)
self._img = self.spec.process(self.image, save_as=self.path)
def delete(self):
if self.exists:

View file

@ -5,7 +5,8 @@ from django.conf import settings
from django.core.files.base import ContentFile
from django.test import TestCase
from models import *
from models import IKModel
from specs import ImageSpec
# Required PIL classes may or may not be available from the root namespace