【问题标题】:Creating django ImageField that is derived from another ImageField in django Models创建从 django 模型中的另一个 ImageField 派生的 django ImageField
【发布时间】:2018-08-11 14:24:45
【问题描述】:

我正在创建一个 django 项目,其中我使用 ImageField 将图片(图像)存储在数据库中...

original_pic = models.ImageField()

另外,我想存储一个包含相同图片的图像(图像) 如original_pic 与另一个ImageField 中的水印作为..

watermark_pic = models.ImageField(default=watermarkImage())

简而言之,我只想在original_pic 上应用算法并使用django 模型将结果保存在watermark_pic

图像加水印的算法(逻辑)如下...

def watermarkImage(filename, text, color, fontfamily):
    image = Image.open(filename).convert('RGBA')
    imageWatermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
    draw = ImageDraw.Draw(imageWatermark)
    width, height = image.size
    font = ImageFont.truetype(fontfamily, int(height / 20))
    textWidth, textHeight = draw.textsize(text, font)
    x = width / 5
    y = height / 6
    draw.text((x, y), text, color, font)
    return Image.alpha_composite(image, imageWatermark)

注意:我知道在图像上应用水印的算法。

【问题讨论】:

  • original_picwatermark_pic 是否属于同一型号?还是在不同的模型中?
  • @JerinPeterGeorge 是的,他们是同一型号
  • 你为什么要这样做?它将生成图像的相同副本..对吗?你需要吗?
  • @JerinPeterGeorge 不,我不想要相同的副本,我想要一个带有水印的图像副本。也就是说,watermark_pic 将包含带有水印的original_pic 的副本就它...
  • 可以添加做水印的逻辑吗?

标签: python django image django-models watermark


【解决方案1】:

我不确定你的水印逻辑是什么样子的。您可以通过重写 save() 方法来实现。无论如何,我为您制作了一个简单的 图像旋转 示例。


from PIL import Image


def rotate_image(image_fp):
    im = Image.open(image_fp)
    rotate = im.rotate(45)
    filename = "rotated_" + image_fp.name
    rotate.save(filename)
    return filename


class MyBaseImageModel(models.Model):
    # your model
    original_pic = models.ImageField()
    watermark_pic = models.ImageField(null=True, blank=True)

    def save(self, *args, **kwargs):
        if not self.pk:
            rotate_img_name = rotate_image(self.original_pic)
            self.watermark_pic = rotate_img_name
        super().save(*args, **kwargs)

创建图像时,您不想为watermark_pic 输入任何值

更新

def watermarkImage(filename, text, color, fontfamily):
    image = Image.open(filename).convert('RGBA')
    imageWatermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
    draw = ImageDraw.Draw(imageWatermark)
    width, height = image.size
    font = ImageFont.truetype(fontfamily, int(height / 20))
    textWidth, textHeight = draw.textsize(text, font)
    x = width / 5
    y = height / 6
    draw.text((x, y), text, color, font)
    my_img = Image.alpha_composite(image, imageWatermark)
    my_img.save('water_'+filename.name)
    return 'water_'+filename.name

class MyBaseImageModel(models.Model):
    # your model
    original_pic = models.ImageField()
    watermark_pic = models.ImageField(null=True, blank=True)

    def save(self, *args, **kwargs):
        if not self.pk:
            rotate_img_name = watermarkImage(your args)
            self.watermark_pic = rotate_img_name
        super().save(*args, **kwargs)

【讨论】:

  • 我已经尝试过了,它在旋转图像时完美运行,但是在尝试添加水印逻辑时出现以下错误...'Image' object has no attribute '_committed' 当调用 save() 方法时。你也有类似的问题吗?
  • 仍有一些错误must be str, not ImageFieldFile
  • 再次进行少量编辑...使用filename.name 而不是filename(更新了答案)
  • 嘿 Jerin 我几乎得到了我所期待的......非常感谢......现在唯一的问题是 watermark_pic 的图像没有存储在我拥有的 /media 中为存储所有图像和上传设置默认值.....再次感谢
  • 棘手的部分是,将'water_'+filename.name 更改为绝对路径MEDIA_URL 目录
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
  • 2018-07-01
相关资源
最近更新 更多