Exemple #1
0
    def rotate(self, angle):
        image_field = self.file
        _rotate(self.path(), angle)

        name = self.path()
        content = open(self.path())
        if image_field.sizes:
            for size in image_field.sizes:
                (w,h) = size
                split = name.rsplit('.',1)
                thumb_name = '%s.%sx%s.%s' % (split[0],w,h,split[1])
                # you can use another thumbnailing function if you like
                thumb_content = generate_thumb(content, size, split[1])
                if os.path.exists(thumb_name):
                    print 'rm', thumb_name
                    os.remove(thumb_name)
                image_field.storage.save(thumb_name, thumb_content)
    def handle(self, *args, **options):
        thumb_fields = []

        for content_type in ContentType.objects.all():
            model = content_type.model_class()
            if not model:  # pragma: nocover
                continue

            for field in model._meta.fields:
                if isinstance(field, ImageWithThumbsField):
                    thumb_fields.append((model, field))

        for model, field in thumb_fields:
            attname = field.get_attname()
            for obj in model.objects.all():
                image = getattr(obj, attname)
                if not image.name:
                    continue

                sizes_to_generate = []
                storage = image.storage

                if not storage.exists(image.name):  # pragma: nocover
                    continue

                for size in image.sizes:
                    width, height = size
                    thumb_name = get_thumb_name(image.name, width, height)

                    if not storage.exists(thumb_name):
                        sizes_to_generate.append(size)

                if sizes_to_generate:
                    image_file = image.file

                    for size in sizes_to_generate:
                        width, height = size
                        thumb_content = generate_thumb(image_file, size)
                        thumb_name = get_thumb_name(image.name, width, height)
                        image.storage.save(thumb_name, thumb_content)