Search code examples
python-3.xwatermark

Python - image watermark


my goal is to create a watermark image, based on a logo (TIF format) and a background image (JPG). I'm using this code:

from PIL import Image 
def watermark_with_transparency(input_image_path,
                                output_image_path,
                                watermark_image_path,
                                position):
    base_image = Image.open(input_image_path)
    watermark = Image.open(watermark_image_path)
    width, height = base_image.size

    transparent = Image.new('RGBA', (width, height), (0,0,0,0))
    transparent.paste(base_image, (0,0))
    transparent.paste(watermark, position, mask=watermark)
    transparent.show()
    transparent.save(output_image_path)

Watermark image is a trasparent TIF. If I run above code, watermark result does not include any logo.

What I'm doing wrong?


Solution

  • from PIL import Image,ImageDraw, ImageFont
    
    path=r'/home/anuj/FIle_server/production/AI_PROJECT/SEQ_001/sq1/frm_seq_v001.001.jpeg'  #This is path of your image
    demo_image = Image.open(path)
    img_width, img_height =demo_image.size
    draw_image = ImageDraw.Draw(demo_image)
    text_image ="HQVFX" #Here you can assign your watermark.
    
    font_image =ImageFont.truetype('/home/anuj/FIle_server/task/WaterBrush-Regular.ttf',50)
    text_width, text_height = draw_image.textsize(text_image,font_image)
    
    font_margin = 10
    
    x = img_width - text_width - font_margin
    y = img_height - text_height - font_margin
    
    draw_image.text((x,y), text_image, font= font_image)
    demo_image.show()
    
    demo_image.save("watermark.jpg")