Skip to content

Commit

Permalink
Merge branch 'image-backend-tweaks' into msi
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Jun 10, 2014
2 parents 08e1b36 + a26c0bc commit 1c2f787
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
2 changes: 2 additions & 0 deletions wagtail/wagtailimages/backends/wand.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .base import BaseImageBackend
from wand.image import Image
from wand.api import library


class WandBackend(BaseImageBackend):
Expand All @@ -10,6 +11,7 @@ def __init__(self, params):

def open_image(self, input_file):
image = Image(file=input_file)
image.wand = library.MagickCoalesceImages(image.wand)
return image

def save_image(self, image, output, format):
Expand Down
37 changes: 19 additions & 18 deletions wagtail/wagtailimages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,15 @@ def _parse_spec_string(self):
# and save the results to self.method_name and self.method_arg
try:
(method_name_simple, method_arg_string) = self.spec.split('-')
method_name = Filter.OPERATION_NAMES[method_name_simple]
self.method_name = Filter.OPERATION_NAMES[method_name_simple]

if method_name_simple in ('max', 'min', 'fill'):
# method_arg_string is in the form 640x480
(width, height) = [int(i) for i in method_arg_string.split('x')]
method_arg = (width, height)
self.method_arg = (width, height)
else:
# method_arg_string is a single number
method_arg = int(method_arg_string)

return method_name, method_arg
self.method_arg = int(method_arg_string)

except (ValueError, KeyError):
raise ValueError("Invalid image filter spec: %r" % self.spec)
Expand All @@ -175,33 +173,36 @@ def process_image(self, input_file, backend_name='default'):
generate an output image with this filter applied, returning it
as another django.core.files.File object
"""
# Get image backend
backend = get_image_backend(backend_name)

# Open image file
if not self.method:
self._parse_spec_string()

# If file is closed, open it
input_file.open('rb')
image = backend.open_image(input_file)
file_format = image.format

# Parse filter spec string
method_name, method_arg = self._parse_spec_string()
method = getattr(backend, self.method_name)

# Run filter
method_f = getattr(backend, method_name)
image = method_f(image, method_arg)
image = method(image, self.method_arg)

# Save
output = StringIO.StringIO()
backend.save_image(image, output, file_format)

# Close input file
# and then close the input file
input_file.close()

# Generate new filename derived from old one, inserting the filter spec string before the extension
filename_noext, ext = os.path.splitext(input_file.name)
output_filename = '.'.join([filename_noext[:60], self.spec]) + ext
# generate new filename derived from old one, inserting the filter spec string before the extension
input_filename_parts = os.path.basename(input_file.name).split('.')
filename_without_extension = '.'.join(input_filename_parts[:-1])
filename_without_extension = filename_without_extension[:60] # trim filename base so that we're well under 100 chars
output_filename_parts = [filename_without_extension, self.spec] + input_filename_parts[-1:]
output_filename = '.'.join(output_filename_parts)

output_file = File(output, name=output_filename)

return File(output, name=output_filename)
return output_file


class AbstractRendition(models.Model):
Expand Down

0 comments on commit 1c2f787

Please sign in to comment.