mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-03 04:54:45 +00:00
Add support for setting global image compression..
quality. Uses an IMAGE_COMPRESSION_QUALITY in the settings.py, which, if set, will set a quality attribute in the image processing backends. Each image processing backend then can use the quality attribute in its "save" method. This has been implemented in both Pillow and Wand backends. It has been tested with image that support this setting (JPG) and images that do not support it (GIF, PNG) and the behavior is consistent. This is an implementation for #163.
This commit is contained in:
parent
b442bab7a7
commit
9bd91848e0
4 changed files with 17 additions and 3 deletions
|
|
@ -1,4 +1,5 @@
|
|||
from django.db import models
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
class InvalidImageBackendError(ImproperlyConfigured):
|
||||
|
|
@ -6,8 +7,13 @@ class InvalidImageBackendError(ImproperlyConfigured):
|
|||
|
||||
|
||||
class BaseImageBackend(object):
|
||||
quality = None
|
||||
|
||||
def __init__(self, params):
|
||||
pass
|
||||
try:
|
||||
self.quality = settings.IMAGE_COMPRESSION_QUALITY
|
||||
except:
|
||||
pass
|
||||
|
||||
def open_image(self, input_file):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -14,7 +14,10 @@ class PillowBackend(BaseImageBackend):
|
|||
return image
|
||||
|
||||
def save_image(self, image, output, format):
|
||||
image.save(output, format)
|
||||
if self.quality:
|
||||
image.save(output, format, quality = self.quality)
|
||||
else:
|
||||
image.save(output, format)
|
||||
|
||||
|
||||
def resize(self, image, size):
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ class WandBackend(BaseImageBackend):
|
|||
|
||||
def save_image(self, image, output, format):
|
||||
image.format = format
|
||||
if self.quality:
|
||||
image.compression_quality = self.quality
|
||||
image.save(file=output)
|
||||
|
||||
def resize(self, image, size):
|
||||
|
|
|
|||
|
|
@ -193,7 +193,7 @@ class Filter(models.Model):
|
|||
input_file.open('rb')
|
||||
image = backend.open_image(input_file)
|
||||
file_format = image.format
|
||||
|
||||
|
||||
method = getattr(backend, self.method_name)
|
||||
|
||||
image = method(image, self.method_arg)
|
||||
|
|
@ -201,6 +201,9 @@ class Filter(models.Model):
|
|||
output = StringIO.StringIO()
|
||||
backend.save_image(image, output, file_format)
|
||||
|
||||
# and then close the input file
|
||||
input_file.close()
|
||||
|
||||
|
||||
# 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('.')
|
||||
|
|
|
|||
Loading…
Reference in a new issue