Esempio n. 1
0
    def testThumbnails(self):
        # Thumbnail
        thumb = Thumbnail(source=PIC_NAME, dest=THUMB_NAME % 1,
                          requested_size=(240, 240))
        self.verify_thumbnail((240, 180), thumb)

        # Cropped thumbnail
        thumb = Thumbnail(source=PIC_NAME, dest=THUMB_NAME % 2,
                          requested_size=(240, 240), opts=['crop'])
        self.verify_thumbnail((240, 240), thumb)

        # Thumbnail with altered JPEG quality
        thumb = Thumbnail(source=PIC_NAME, dest=THUMB_NAME % 3,
                          requested_size=(240, 240), quality=95)
        self.verify_thumbnail((240, 180), thumb)
Esempio n. 2
0
 def testFilelikeDest(self):
     # Thumbnail
     filelike_dest = StringIO()
     thumb = Thumbnail(source=PIC_NAME,
                       dest=filelike_dest,
                       requested_size=(240, 240))
     self.verify_thumbnail((240, 180), thumb)
Esempio n. 3
0
    def testRegeneration(self):
        # Create thumbnail
        thumb_name = THUMB_NAME % 4
        thumb_size = (240, 240)
        Thumbnail(source=PIC_NAME, dest=thumb_name, requested_size=thumb_size)
        self.images_to_delete.add(thumb_name)
        thumb_mtime = os.path.getmtime(thumb_name)
        time.sleep(1)

        # Create another instance, shouldn't generate a new thumb
        Thumbnail(source=PIC_NAME, dest=thumb_name, requested_size=thumb_size)
        self.assertEqual(os.path.getmtime(thumb_name), thumb_mtime)

        # Recreate the source image, then see if a new thumb is generated
        Image.new('RGB', PIC_SIZE).save(PIC_NAME, 'JPEG')
        Thumbnail(source=PIC_NAME, dest=thumb_name, requested_size=thumb_size)
        self.assertNotEqual(os.path.getmtime(thumb_name), thumb_mtime)
Esempio n. 4
0
def create_thumbnails(obj, content, size=None, thumb_name=None):
    sizes = size and [size] or obj.field.thumb_sizes

    for size in sizes:
        img = StringIO()
        content.seek(0)
        if not isinstance(content, StringIO):
            content = StringIO(content.read(content.size))
        Thumbnail(content, size, dest=img, opts=obj.field.thumb_options)
        th_name = thumb_name or obj.build_thumbnail_name(obj.name, size)
        obj.storage.save(th_name, ContentFile(img.read()))
Esempio n. 5
0
 def save(self, name, content, *args, **kwargs):
     new_content = StringIO()
     # Build the Thumbnail kwargs.
     thumbnail_kwargs = {}
     for k, argk in BASE_ARGS.items():
         if not k in self.field.thumbnail:
             continue
         thumbnail_kwargs[argk] = self.field.thumbnail[k]
     Thumbnail(source=content, dest=new_content, **thumbnail_kwargs)
     new_content = ContentFile(new_content.read())
     super(ThumbnailFieldFile, self).save(name, new_content, *args,
                                          **kwargs)
Esempio n. 6
0
 def testRGBA(self):
     # RGBA image
     rgba_pic_name = os.path.join(settings.MEDIA_ROOT,
                                  'sorl-thumbnail-test_rgba_source.png')
     Image.new('RGBA', PIC_SIZE).save(rgba_pic_name)
     self.images_to_delete.add(rgba_pic_name)
     # Create thumb and verify it's still RGBA
     rgba_thumb_name = os.path.join(settings.MEDIA_ROOT,
                                    'sorl-thumbnail-test_rgba_dest.png')
     thumb = Thumbnail(source=rgba_pic_name,
                       dest=rgba_thumb_name,
                       requested_size=(240, 240))
     self.verify_thumbnail((240, 180), thumb, expected_mode='RGBA')