2009-01-08 21:11:15 +00:00
|
|
|
""" ImageKit utility functions """
|
|
|
|
|
|
2011-09-25 03:09:49 +00:00
|
|
|
import tempfile, types
|
|
|
|
|
from django.utils.functional import wraps
|
|
|
|
|
from lib import Image
|
|
|
|
|
|
2009-01-08 20:04:20 +00:00
|
|
|
|
|
|
|
|
def img_to_fobj(img, format, **kwargs):
|
|
|
|
|
tmp = tempfile.TemporaryFile()
|
2011-06-17 20:13:20 +00:00
|
|
|
img.convert('RGB').save(tmp, format, **kwargs)
|
2009-01-08 20:04:20 +00:00
|
|
|
tmp.seek(0)
|
2009-01-08 21:11:15 +00:00
|
|
|
return tmp
|
2011-09-22 00:12:49 +00:00
|
|
|
|
|
|
|
|
|
2011-09-22 13:20:37 +00:00
|
|
|
def get_spec_files(instance):
|
2011-10-04 02:51:03 +00:00
|
|
|
try:
|
|
|
|
|
ik = getattr(instance, '_ik')
|
|
|
|
|
except AttributeError:
|
|
|
|
|
return []
|
|
|
|
|
else:
|
|
|
|
|
return [getattr(instance, n) for n in ik.spec_file_names]
|
2011-09-25 03:09:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def open_image(target):
|
|
|
|
|
img = Image.open(target)
|
|
|
|
|
img.copy = types.MethodType(_wrap_copy(img.copy), img, img.__class__)
|
|
|
|
|
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _wrap_copy(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
|
def copy(self):
|
|
|
|
|
img = f()
|
|
|
|
|
try:
|
|
|
|
|
img.app = self.app
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
|
|
|
|
try:
|
|
|
|
|
img._getexif = self._getexif
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
|
|
|
|
return img
|
|
|
|
|
return copy
|
|
|
|
|
|