Create watermark and resize images with Python

After holiday, I have many pictures that needs to be uploaded and all of them should have watermark. Watermark in image is one thing that I could’ve done it with photoshop when I was in school. But I’m not gonna waste my time to do that one by one instead I’m doing with this simple python script. The library that I used was PIL (Python Image Library).

This one I’ve done with custom font so you can choose your favorite font in your images. This script will be run two jobs to edit every images you have in one directory. The first thing it gonna resize your image based on what I already defined inside the script, you might want to change the basewidth variable if you want and after resizing, the script will put the watermark at the corner of your image.

For the font, I was just searching for the free license in dafont.com

Make sure you download the .ttf extension, not sure whether the other extensions will work.

#!/usr/bin/env python
import PIL
from PIL import Image, ImageDraw, ImageFont

def resize():
  image = Image.open('IMG_0495.JPG')
  width, height = image.size
  basewidth = 800
  wpercent = (basewidth / float(image.size[0]))
  hsize = int((float(image.size[1]) * float(wpercent)))
  image = image.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
  image.save('TES.JPG')
def watermark():
  image = Image.open('TES.JPG')
  width, height = image.size
  draw = ImageDraw.Draw(image)
  text = "Your credit written here"
  font = ImageFont.truetype('Downloads/Capture_it.ttf', 15)
  textwidth, textheight = draw.textsize(text, font)
  # calculate the x,y coordinates of the text
  margin = 5
  x = width - textwidth - margin
  y = height - textheight - margin
  # draw watermark in the bottom right corner
  draw.text((x, y), text)
  image.save('TES.JPG')
def main():
  resize()
  watermark()
main()