0

I have a task of watermarking the image using python pillow. The function is supposed to take in two parameters: 1. Name of an image file and 2.Message - This is text that should be watermarked onto the image

The function should save the watermarked image with the name of the original file with a “_zz” appended to the of the name before the period.

I used the 'classic" python pillow approach:

def watermark (name, message):
    from PIL import Image, ImageDraw, ImageFont
    im = Image.open(name)
    width, height = im.size
    draw = ImageDraw.Draw(im)
    text = message
    font = ImageFont.truetype('arial.ttf', 36)
    textwidth, textheight = draw.textsize(text, font)
    margin = 10
    x = width - textwidth - margin
    y = height - textheight - margin
    draw.text((x, y), text, font=font)

    im.show()
    im.save(name+'_zz') # incorrect of course

I can't figure out how to save the file with '_zz" amended to file name only and not to the extension. I am new to Python, please be patient with me...

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • 1
    https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format –  May 06 '21 at 16:29

1 Answers1

0

Cool! I am answering my own question... Very simple: `

    e=name.split(".")[-1]
    n=name.split(".")[0]
    wm_name=(n+'_zz'+'.'+e)

    im.save(zz_name)

`