Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Format Preservation with minimal alterations #135

Closed
wants to merge 9 commits into from

4 participants

@pygeek

This pull request adds the ability to preserve the file type of the image being converted into a thumbnail depending on the boolean settings.THUMBNAIL_PRESERVE_FORMAT, which is set to False by default.

This pull request gracefully degrades and defaults to using settings.THUMBNAIL_FORMAT. Applications that currently use sorl should not notice any difference in functionality unless settings.THUMBNAIL_PRESERVE_FORMAT is set explicitly set to True.

This pull request is desirable for anyone developing a medium scale application that relies on sorl, as the dilemma of preserving transparent PNGs as PNGs while optimizing JPGs arises quickly. I have several colleagues who have written this functionality into their application or have created their own backend to handle this case; I feel this is basic functionality that should be included in the sorl repo.

Thank You and Best Regards,
~pygeek

pygeek added some commits
@pygeek pygeek Update sorl/thumbnail/conf/defaults.py
Boolean to preserve thumbnail format added. Defaulted to False
4a2265e
@pygeek pygeek File type preservation added.
There are instances where file_type preservation is desired at the base level.
I, therefore, added ability to preserve file type in thumbnail if settings.THUMBNAIL_PRESERVE_FORMAT = True; it is set to False by default.
826f650
@pygeek pygeek Update README.rst 8b70b32
@pygeek pygeek Update README.rst 9c2129b
@pygeek pygeek Update README.rst 2694f92
@pygeek pygeek Update README.rst e90bb2f
@pygeek pygeek Update setup.py e5505bb
@pygeek pygeek Update sorl/thumbnail/base.py 9b9930a
@thenewguy

I think this is a good idea. I think it would be better to test for image type instead of checking the file extension though.

There is a python module for testing image type: http://docs.python.org/2/library/imghdr.html. The convert engines could also provide a method to test for image types if they could do it faster, more reliably, or better in some way.

@tezro

Yes. It should be the default option.

@mariocesar mariocesar was assigned
@mariocesar
Owner

@pygeek nice! Right now is not possible to automerge this, would you please review your pull request against the current master branch? I will be glad to merge this ASAP

@mariocesar
Owner

This is fixed on the master branch, thanks :)

@mariocesar mariocesar closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 11, 2012
  1. @pygeek

    Update sorl/thumbnail/conf/defaults.py

    pygeek authored
    Boolean to preserve thumbnail format added. Defaulted to False
  2. @pygeek

    File type preservation added.

    pygeek authored
    There are instances where file_type preservation is desired at the base level.
    I, therefore, added ability to preserve file type in thumbnail if settings.THUMBNAIL_PRESERVE_FORMAT = True; it is set to False by default.
  3. @pygeek

    Update README.rst

    pygeek authored
  4. @pygeek

    Update README.rst

    pygeek authored
  5. @pygeek

    Update README.rst

    pygeek authored
  6. @pygeek

    Update README.rst

    pygeek authored
  7. @pygeek

    Update setup.py

    pygeek authored
Commits on Dec 12, 2012
  1. @pygeek

    Update sorl/thumbnail/base.py

    pygeek authored
Commits on Jan 24, 2013
  1. @pygeek

    Update README.rst

    pygeek authored
This page is out of date. Refresh to see the latest.
View
25 README.rst
@@ -1,20 +1,13 @@
-sorl-thumbnail
-==============
+django-thumbnail
+================
-Thumbnails for Django. Totally rewritten.
+A fork of sorl-thumbnail; created due to its inactivity.
-Features at a glance
---------------------
-- Storage support
-- Pluggable Engine support (ImageMagick, PIL, pgmagick included)
-- Pluggable Key Value Store support (cached db, redis)
-- Pluggable Backend support
-- Admin integration with possibility to delete
-- Dummy generation (placeholders)
-- Flexible, simple syntax, generates no html
-- ImageField for model that deletes thumbnails
-- CSS style cropping options
-- Margin calculation for vertical positioning
+pip install django-thumbnail
-Read more in `the documentation (latest version) <http://sorl-thumbnail.rtfd.org/>`_
+---------------------------------------------------------
+
+Format preservation has been added. To preserve format set settings.THUMBNAIL_PRESERVE_FORMAT = True.
+Gracefully degrades and defaults to using settings.THUMBNAIL_FORMAT.
+Applications that currently use sorl should not notice any difference in functionality unless settings.THUMBNAIL_PRESERVE_FORMAT is set explicitly set to True.
View
10 setup.py
@@ -10,14 +10,14 @@ def run(self):
setup(
- name='sorl-thumbnail',
+ name='django-thumbnail',
version=sorl.__version__,
- description='Thumbnails for Django',
+ description='Thumbnailing in Django',
long_description=open('README.rst').read(),
- author='Mikko Hellsing',
- author_email='mikko@aino.se',
+ author='Clinton Christian',
+ author_email='pygeek@me.com',
license='BSD',
- url='https://github.com/sorl/sorl-thumbnail',
+ url='https://github.com/pygeek/django-thumbnail',
packages=find_packages(exclude=['tests', 'tests.*']),
platforms='any',
zip_safe=False,
View
25 sorl/thumbnail/base.py
@@ -1,3 +1,5 @@
+import re
+
from sorl.thumbnail.conf import settings, defaults as default_settings
from sorl.thumbnail.helpers import tokey, serialize
from sorl.thumbnail.images import ImageFile
@@ -16,6 +18,7 @@ class ThumbnailBackend(object):
The main class for sorl-thumbnail, you can subclass this if you for example
want to change the way destination filename is generated.
"""
+
default_options = {
'format': settings.THUMBNAIL_FORMAT,
'quality': settings.THUMBNAIL_QUALITY,
@@ -29,6 +32,23 @@ class ThumbnailBackend(object):
('orientation', 'THUMBNAIL_ORIENTATION'),
)
+ file_extension = lambda inst, file_: str(file_).split('.')[-1].lower()
+
+ def _get_format(self, file_):
+ file_extension = self.file_extension(file_)
+
+ is_jpeg = re.match('jpg|jpeg', file_extension)
+ is_png = re.match('png', file_extension)
+
+ if is_jpeg:
+ format_ = 'JPEG'
+ elif is_png:
+ format_ = 'PNG'
+ else:
+ format_ = default_settings.THUMBNAIL_FORMAT
+
+ return str(format_)
+
def get_thumbnail(self, file_, geometry_string, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
@@ -36,6 +56,11 @@ def get_thumbnail(self, file_, geometry_string, **options):
secondly it will create it.
"""
source = ImageFile(file_)
+
+ #preserve image filetype
+ if settings.THUMBNAIL_PRESERVE_FORMAT:
+ self.default_options['format'] = self._get_format(file_)
+
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
# For the future I think it is better to add options only if they
View
2  sorl/thumbnail/conf/defaults.py
@@ -51,6 +51,8 @@
# Make sure the backend can handle the format you specify
THUMBNAIL_FORMAT = 'JPEG'
+THUMBNAIL_PRESERVE_FORMAT = False
+
# Colorspace, backends are required to implement: RGB, GRAY
# Setting this to None will keep the original colorspace.
THUMBNAIL_COLORSPACE = 'RGB'
Something went wrong with that request. Please try again.