Search code examples
pythondjangoimagedjango-modelswatermark

Creating django ImageField that is derived from another ImageField in django Models


I am creating a django Project where I had Stored a Picture(Image) in database using ImageField as...

original_pic = models.ImageField()

Also, I want to Store an Image which will Contain the Same Picture(Image) as original_pic with Watermark in another ImageField as..

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

In short, I just want to Apply Algorithm on original_pic and save the result in watermark_pic using django models

Algorithm(Logic) for Applying Watermark to image is as follows...

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)

Note: I know the Algorithm for Applying watermark on Image.


Solution

  • I'm not sure how you watermark logic looks like. You could do it by overriding the save() method. Anyway, I made a simple working image rotation example for you.

    
    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)

    When you create the image, you dont want to put any values for watermark_pic

    UPDATE

    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)