django-imagekit/imagekit/generators.py

94 lines
3.2 KiB
Python
Raw Normal View History

from django.conf import settings
from hashlib import md5
2012-02-12 04:48:54 +00:00
import os
import pickle
2012-05-12 19:45:08 +00:00
from .lib import StringIO
2012-04-20 03:01:07 +00:00
from .processors import ProcessorPipeline
2012-04-20 01:26:42 +00:00
from .utils import (img_to_fobj, open_image, IKContentFile, extension_to_format,
suggest_extension, UnknownExtensionError)
2012-02-12 04:48:54 +00:00
class SpecFileGenerator(object):
2012-04-10 01:25:46 +00:00
def __init__(self, processors=None, format=None, options=None,
2012-02-12 04:48:54 +00:00
autoconvert=True, storage=None):
self.processors = processors
self.format = format
2012-04-10 01:25:46 +00:00
self.options = options or {}
2012-02-12 04:48:54 +00:00
self.autoconvert = autoconvert
self.storage = storage
def get_processors(self, source_file):
processors = self.processors
if callable(processors):
processors = processors(source_file)
return processors
2012-02-12 04:48:54 +00:00
def process_content(self, content, filename=None, source_file=None):
img = open_image(content)
original_format = img.format
# Run the processors
processors = self.get_processors(source_file)
2012-02-12 04:48:54 +00:00
img = ProcessorPipeline(processors or []).process(img)
options = dict(self.options or {})
# Determine the format.
format = self.format
2012-04-21 03:26:16 +00:00
if filename and not format:
2012-02-12 04:48:54 +00:00
# Try to guess the format from the extension.
extension = os.path.splitext(filename)[1].lower()
if extension:
try:
format = extension_to_format(extension)
except UnknownExtensionError:
pass
2012-04-21 03:26:16 +00:00
format = format or img.format or original_format or 'JPEG'
2012-02-12 04:48:54 +00:00
imgfile = img_to_fobj(img, format, **options)
2012-04-21 03:30:30 +00:00
content = IKContentFile(filename, imgfile.read(), format=format)
2012-02-12 04:48:54 +00:00
return img, content
def generate_filename(self, source_file):
source_filename = source_file.name
filename = None
if source_filename:
hash = md5(''.join([
pickle.dumps(self.get_processors(source_file)),
self.format,
pickle.dumps(self.options),
str(self.autoconvert),
])).hexdigest()
extension = suggest_extension(source_filename, self.format)
filename = os.path.normpath(os.path.join(
settings.IMAGEKIT_CACHE_DIR,
os.path.splitext(source_filename)[0],
'%s%s' % (hash, extension)))
return filename
2012-02-12 04:48:54 +00:00
def generate_file(self, filename, source_file, save=True):
"""
Generates a new image file by processing the source file and returns
the content of the result, ready for saving.
"""
if source_file: # TODO: Should we error here or something if the source_file doesn't exist?
# Process the original image file.
try:
fp = source_file.storage.open(source_file.name)
except IOError:
return
fp.seek(0)
fp = StringIO(fp.read())
img, content = self.process_content(fp, filename, source_file)
if save:
storage = self.storage or source_file.storage
storage.save(filename, content)
2012-02-14 02:16:45 +00:00
return content