Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django ImageKit:添加水印并创建缩略图_Python_Django_Django Imagekit - Fatal编程技术网

Python Django ImageKit:添加水印并创建缩略图

Python Django ImageKit:添加水印并创建缩略图,python,django,django-imagekit,Python,Django,Django Imagekit,我需要适合用户上传的图像到1000px,并把水印。我还需要创建缩略图(没有水印) 问题是缩略图是从已经处理过的图像创建的。 如果不应保存原始图像,如何从原始图像创建缩略图?我使用easy_缩略图和信号解决了此问题: # settings.py THUMBNAIL_ALIASES = { 'gallery': { 'small': {'size': (200, 200), 'crop': True}, }, } THUMBNAIL_PROCESSORS = easy

我需要适合用户上传的图像到1000px,并把水印。我还需要创建缩略图(没有水印)

问题是缩略图是从已经处理过的图像创建的。
如果不应保存原始图像,如何从原始图像创建缩略图?

我使用easy_缩略图和信号解决了此问题:

# settings.py
THUMBNAIL_ALIASES = {
    'gallery': {
        'small': {'size': (200, 200), 'crop': True},
    },
}
THUMBNAIL_PROCESSORS = easy_thumbnails_defaults.THUMBNAIL_PROCESSORS + (
    'gallery.models.watermark_processor',
)

# gallery/models.py
def watermark_processor(image, watermark=False, **kwargs):
    if watermark:
        draw = ImageDraw.Draw(image)
        draw.line((0, 0) + image.size, fill=128)
        draw.line((0, image.size[1], image.size[0], 0), fill=128)
    return image

class Photo(models.Model):
    image = ThumbnailerImageField(upload_to='photo',
                                  resize_source={
                                      'size': (1000, 1000),
                                      'watermark': True,
                                  })

@receiver(pre_save, sender=Photo)
def make_thumbnail(sender, instance, **kwargs):
    easy_thumbnails.files.generate_all_aliases(instance.image, False)
不幸的是,将此信号技巧用于ImageKit(如
instance.thumboil.generate()
)会引发I/O错误

# settings.py
THUMBNAIL_ALIASES = {
    'gallery': {
        'small': {'size': (200, 200), 'crop': True},
    },
}
THUMBNAIL_PROCESSORS = easy_thumbnails_defaults.THUMBNAIL_PROCESSORS + (
    'gallery.models.watermark_processor',
)

# gallery/models.py
def watermark_processor(image, watermark=False, **kwargs):
    if watermark:
        draw = ImageDraw.Draw(image)
        draw.line((0, 0) + image.size, fill=128)
        draw.line((0, image.size[1], image.size[0], 0), fill=128)
    return image

class Photo(models.Model):
    image = ThumbnailerImageField(upload_to='photo',
                                  resize_source={
                                      'size': (1000, 1000),
                                      'watermark': True,
                                  })

@receiver(pre_save, sender=Photo)
def make_thumbnail(sender, instance, **kwargs):
    easy_thumbnails.files.generate_all_aliases(instance.image, False)