Commits

George Notaras  committed ed17406

Several improvements regarding support for thumbnail options

  • Participants
  • Parent commits 82a8a7c

Comments (0)

Files changed (3)

File src/thumbnail_works/fields.py

 
 
 
-class Thumbnail:
+class ThumbnailSpec:
+    """Thumbnail specification.
     
-    MANDATORY_OPTIONS = ['size']
+    """
+    
+    #MANDATORY_OPTIONS = ['size']
     DEFAULT_OPTIONS = {
         'size': None,
         'sharpen': False,
         """
         name: the thumbnail name as set in the 'thumbnails' dictionary
         options: the thumbnail options as set in the 'thumbnails' dictionary
-
+        source: an instance of the EnhancedImageFieldFile
+        
         """
         if self._options_are_valid(options):
             self.options = self._get_options(options)
         return thumb_options
     
     def _options_are_valid(self, options):
-        if not options.has_key('size'):
-            raise ThumbnailOptionError('Thumbnail is missing the mandatory `size` option')
+        #if not options.has_key('size'):
+        #    raise ThumbnailOptionError('Thumbnail is missing the mandatory `size` option')
         for option in options.keys():
             if option not in self.DEFAULT_OPTIONS.keys():
                 raise ThumbnailOptionError('Invalid thumbnail option `%s`' % option)
     
         photo.avatar.url
     
-    For information about the attributes of each thimbnail object, read the
-    ``Thumbnail`` docstring.
+    For information about the attributes of each thumbnail object, read the
+    ``ThumbnailSpec`` docstring.
     
     """
     
           deleted from the filesystem.
         
         If the source image has been saved and thumbnails have been specified,
-        instanciate the latter using the ``Thumbnail`` class and add them as
-        attributes to the ``EnhancedImageFieldFile`` object.
+        instanciate the latter using the ``ThumbnailSpec`` class and add them
+        as attributes to the ``EnhancedImageFieldFile`` object.
         
         """
         super(EnhancedImageFieldFile, self).__init__(*args, **kwargs)
         # exist and the source image has been saved.
         if self._committed and self.field.thumbnails:
             for thumb_name, thumb_options in self.field.thumbnails.items():
-                thumb_obj = Thumbnail(thumb_name, thumb_options, self)
-                setattr(self, thumb_name, thumb_obj)
+                thumb_spec = ThumbnailSpec(thumb_name, thumb_options, self)
+                setattr(self, thumb_name, thumb_spec)
+    
+    #def process_image(self, content, options):
     
     def save(self, name, content, save=True):
         source_filename = name
         # Before saving, resize the source image if the resize_source has been
         # set.
         if self.field.resize_source is not None:
-            # This also saves the source image using the THUMBNAILS_FORMAT 
+            thumb_spec = ThumbnailSpec('dummy', self.field.resize_source, self)
+            # This also saves the source image using the THUMBNAILS_FORMAT
             processed_content = process_content_as_image(
-                content, resize_to=self.field.resize_source, sharpen=True)
+                content, options=thumb_spec.options)
         elif settings.THUMBNAILS_FORCE_SOURCE_FORMAT:
-            processed_content = process_content_as_image(
-                content, sharpen=True, format=settings.THUMBNAILS_FORCE_SOURCE_FORMAT)
+            thumb_spec = ThumbnailSpec('dummy', {}, self)
+            processed_content = process_content_as_image(content, thumb_spec.options, format=settings.THUMBNAILS_FORCE_SOURCE_FORMAT)
         else:
             processed_content = content
         super(EnhancedImageFieldFile, self).save(source_filename, processed_content, save)
         
         # Generate thumbnails
         if self.field.thumbnails:
-            for thumb_name, thumb_size in self.field.thumbnails.items():
+            for thumb_name, thumb_options in self.field.thumbnails.items():
                 #new_content = copy.deepcopy(content)
                 print "Doing: ", thumb_name, thumb_size
-
+                
+                thumb_spec = ThumbnailSpec(thumb_name, thumb_options, self)
+                
                 processed_content = process_content_as_image(
-                    content, resize_to=thumb_size, sharpen=True)
+                    content, thumb_spec.options)
                 thumb_path = make_thumbnail_path(source_path, thumb_name)
                 print thumb_path
                 thumb_path_saved = self.storage.save(thumb_path, processed_content)

File src/thumbnail_works/image_processors.py

 from PIL import Image, ImageFilter
 
 
-def resize(im, size):
+def resize(im, size, upscale):
     """
     The resize happens only if any of the following conditions is met:
     
     if width_source < height_source:
         landscape_orientation = False
     
+    #TODO: check the upscale
+    
     # Determine if resize is needed. (Also creates the temporary resized file)
     if width_source > width_req or height_source > height_req:
         # Do resize

File src/thumbnail_works/utils.py

     return os.path.join(root_dir, '%s.%s.%s' % (base_filename, thumbnail_name, ext))
 
 
-def process_content_as_image(content, size=None, sharpen=False,
-        format=settings.THUMBNAILS_FORMAT):
+def process_content_as_image(content, options=None, format=None):
+    
+    if options is None and format is None:
+        return content
+    elif format is None:
+        # Use the default format
+        format = settings.THUMBNAILS_FORMAT
     
     # Image.open() accepts a file-like object, but it is needed
     # to rewind it back to be able to get the data,
         im = im.convert('RGB')
     
     # Process
-    if size is not None:
-        new_size = get_width_height_from_string(size)
-        im = image_processors.resize(im, new_size)
-    if sharpen:
-        im = image_processors.sharpen(im)
+    if options:
+        size = options['size']
+        upscale = options['upscale']
+        if size is not None:
+            new_size = get_width_height_from_string(size)
+            im = image_processors.resize(im, new_size, upscale)
+        
+        sharpen = options['sharpen']
+        if sharpen:
+            im = image_processors.sharpen(im)
+        
+        detail = options['detail']
+        if detail:
+            im = image_processors.detail(im)
     
     # Save image data
     buffer = StringIO.StringIO()