Exemple #1
0
    def save(self, force_insert=False, force_update=False):
        """
        Save the photo.  Overrides the model's default ``save`` method
        to save the original photo in various dimensions.  Each size is
        used in different parts of the site.  The original photo is also
        kept.
        """
        super(Photo, self).save(force_insert, force_update)

        self._set_orientation()
        medium_size = Photo.MEDIUM_LANDSCAPE_SIZE
        if not self.is_landscape:
            medium_size = Photo.MEDIUM_PORTRAIT_SIZE
        image_basename = '%s.jpg' % hashlib.sha1(str(self.id)).hexdigest()
        im = Image.open(self.original.path)
        # Workaround for a problem in the PIL JPEG library:
        # http://mail.python.org/pipermail/image-sig/1999-August/000816.html.
        ImageFile.MAXBLOCK = 1000000

        image_presets = {
            'medium': {
                'field': self.medium,
                'size': medium_size,
                'upload_directory': Photo.MEDIUM_UPLOAD_DIRECTORY
            },
            'listing': {
                'field': self.listing,
                'size': Photo.LISTING_SIZE,
                'upload_directory': Photo.LISTING_UPLOAD_DIRECTORY
            },
            'thumbnail': {
                'field': self.thumbnail,
                'size': Photo.THUMBNAIL_SIZE,
                'upload_directory': Photo.THUMBNAIL_UPLOAD_DIRECTORY
            },
        }
        for preset_name, preset in image_presets.items():
            if preset_name == 'medium':
                image = im
                image.thumbnail(preset['size'], Image.ANTIALIAS)
            else:
                image = create_thumbnail(im, preset['size'])
            image.save(os.path.join(settings.MEDIA_ROOT,
                                    preset['upload_directory'],
                                    image_basename),
                       format="JPEG",
                       quality=85,
                       optimize=True)
            preset['file'] = os.path.join(preset['upload_directory'],
                                          image_basename)

        self.medium = image_presets['medium']['file']
        self.listing = image_presets['listing']['file']
        self.thumbnail = image_presets['thumbnail']['file']

        super(Photo, self).save(force_insert, force_update)
Exemple #2
0
 def save(self, force_insert=False, force_update=False):
     """
     Save the collection.  Overrides the model's default ``save``
     method to save the key photo for the collection at the correct
     size.
     """
     super(Collection, self).save(force_insert, force_update)
     im = Image.open(self.key_photo.path)
     if not im.size == Collection.KEY_PHOTO_SIZE:
         image = create_thumbnail(im, Collection.KEY_PHOTO_SIZE)
         image.save(self.key_photo.path, format="JPEG", quality=85,
             optimize=True)
     super(Collection, self).save(force_insert, force_update)
Exemple #3
0
 def save(self, force_insert=False, force_update=False):
     """
     Save the collection.  Overrides the model's default ``save``
     method to save the key photo for the collection at the correct
     size.
     """
     super(Collection, self).save(force_insert, force_update)
     im = Image.open(self.key_photo.path)
     if not im.size == Collection.KEY_PHOTO_SIZE:
         image = create_thumbnail(im, Collection.KEY_PHOTO_SIZE)
         image.save(self.key_photo.path,
                    format="JPEG",
                    quality=85,
                    optimize=True)
     super(Collection, self).save(force_insert, force_update)
Exemple #4
0
    def save(self, force_insert=False, force_update=False):
        """
        Save the photo.  Overrides the model's default ``save`` method
        to save the original photo in various dimensions.  Each size is
        used in different parts of the site.  The original photo is also
        kept.
        """
        super(Photo, self).save(force_insert, force_update)

        self._set_orientation()
        medium_size = Photo.MEDIUM_LANDSCAPE_SIZE
        if not self.is_landscape:
            medium_size = Photo.MEDIUM_PORTRAIT_SIZE
        image_basename = '%s.jpg' % hashlib.sha1(str(self.id)).hexdigest()
        im = Image.open(self.original.path)
        # Workaround for a problem in the PIL JPEG library:
        # http://mail.python.org/pipermail/image-sig/1999-August/000816.html.
        ImageFile.MAXBLOCK = 1000000

        image_presets = {
            'medium': {'field': self.medium, 'size': medium_size,
                'upload_directory': Photo.MEDIUM_UPLOAD_DIRECTORY},
            'listing': {'field': self.listing, 'size': Photo.LISTING_SIZE,
                'upload_directory': Photo.LISTING_UPLOAD_DIRECTORY},
            'thumbnail': {'field': self.thumbnail, 'size': Photo.THUMBNAIL_SIZE,
                'upload_directory': Photo.THUMBNAIL_UPLOAD_DIRECTORY},
        }
        for preset_name, preset in image_presets.items():
            if preset_name == 'medium':
                image = im
                image.thumbnail(preset['size'], Image.ANTIALIAS)
            else:
                image = create_thumbnail(im, preset['size'])
            image.save(os.path.join(settings.MEDIA_ROOT,
                preset['upload_directory'], image_basename), format="JPEG",
                quality=85, optimize=True)
            preset['file'] = os.path.join(preset['upload_directory'],
                image_basename)

        self.medium = image_presets['medium']['file']
        self.listing = image_presets['listing']['file']
        self.thumbnail = image_presets['thumbnail']['file']

        super(Photo, self).save(force_insert, force_update)
 def save(self, force_insert=False, force_update=False):
     """
     Save a thumbnail for the uploaded file.  If it's an image the
     thumbnail contains a crop of the image itself, framed within a
     pretty border.  Any file other than an image just gets a default
     icon.
     """
     # The file model object needs to be saved first before the file
     # itself can be accessed.
     super(File, self).save(force_insert, force_update)
     # If the uploaded file is an image, create a thumbnail based on
     # the image itself.  If it's not an image, use the default
     # thumbnail.
     try:
         # Open the uploaded image and create a thumbnail.
         im = Image.open(self.item.path)
         thumbnail_image = create_thumbnail(im, File.THUMBNAIL_SIZE)
         thumbnail_basename = "%d.png" % self.id
         # Create a new image the same size as the frame image, with
         # the thumbnail centred within it.
         image_frame = Image.open(
             os.path.join(settings.MEDIA_ROOT, File.IMAGE_FRAME_FILE))
         thumbnail_layer = Image.new('RGBA', image_frame.size)
         vertical_pos = (image_frame.size[0] - thumbnail_image.size[0]) / 2
         horizontal_pos = (image_frame.size[1] -
                           thumbnail_image.size[1]) / 2
         thumbnail_layer.paste(thumbnail_image,
                               (vertical_pos, horizontal_pos))
         # Layer the thumbnail underneath the frame image.
         thumbnail = Image.composite(image_frame, thumbnail_layer,
                                     image_frame)
         thumbnail.save(os.path.join(settings.MEDIA_ROOT,
                                     File.THUMBNAIL_UPLOAD_DIRECTORY,
                                     thumbnail_basename),
                        format="PNG",
                        optimize=True)
         self.thumbnail = os.path.join(File.THUMBNAIL_UPLOAD_DIRECTORY,
                                       thumbnail_basename)
     except IOError:
         # The uploaded file isn't an image format supported by PIL.
         self.thumbnail = File.DEFAULT_ICON_FILE
     super(File, self).save(force_insert, force_update)
Exemple #6
0
 def save(self, force_insert=False, force_update=False):
     """
     Save a thumbnail for the uploaded file.  If it's an image the
     thumbnail contains a crop of the image itself, framed within a
     pretty border.  Any file other than an image just gets a default
     icon.
     """
     # The file model object needs to be saved first before the file 
     # itself can be accessed.
     super(File, self).save(force_insert, force_update)
     # If the uploaded file is an image, create a thumbnail based on
     # the image itself.  If it's not an image, use the default
     # thumbnail.
     try:
         # Open the uploaded image and create a thumbnail.
         im = Image.open(self.item.path)
         thumbnail_image = create_thumbnail(im, File.THUMBNAIL_SIZE)
         thumbnail_basename = "%d.png" % self.id
         # Create a new image the same size as the frame image, with
         # the thumbnail centred within it.
         image_frame = Image.open(os.path.join(settings.MEDIA_ROOT,
             File.IMAGE_FRAME_FILE))
         thumbnail_layer = Image.new('RGBA', image_frame.size)
         vertical_pos = (image_frame.size[0] - thumbnail_image.size[0]) / 2
         horizontal_pos = (image_frame.size[1] - thumbnail_image.size[1]) / 2
         thumbnail_layer.paste(thumbnail_image, (vertical_pos,
             horizontal_pos))
         # Layer the thumbnail underneath the frame image.
         thumbnail = Image.composite(image_frame, thumbnail_layer,
             image_frame)
         thumbnail.save(os.path.join(settings.MEDIA_ROOT,
             File.THUMBNAIL_UPLOAD_DIRECTORY, thumbnail_basename),
             format="PNG", optimize=True)
         self.thumbnail = os.path.join(File.THUMBNAIL_UPLOAD_DIRECTORY,
             thumbnail_basename)
     except IOError:
         # The uploaded file isn't an image format supported by PIL.
         self.thumbnail = File.DEFAULT_ICON_FILE
     super(File, self).save(force_insert, force_update)