Skip to content

Instantly share code, notes, and snippets.

@Quasimondo
Created January 28, 2021 09:33
Show Gist options
  • Save Quasimondo/ed3f7f72f6e7686dd6abb8dea638d087 to your computer and use it in GitHub Desktop.
Save Quasimondo/ed3f7f72f6e7686dd6abb8dea638d087 to your computer and use it in GitHub Desktop.
'''
Usage:
python quickwatermark.py [path to folder that contains images to waternark]
This will go through all the files in that folder, try to open them and add
the filename as text on top of the image. The watermarked images will be stored
in a subfolder of the chosen folder called "watermarked"
'''
import os
from PIL import Image, ImageDraw
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('folder')
args = parser.parse_args()
folder = args.folder
if os.path.exists(folder):
if not folder[-1]=="/":
folder += "/"
os.makedirs(folder+"watermarked",exist_ok=True)
for filename in os.listdir(folder):
if not os.path.isdir(folder+filename):
try:
im = Image.open(folder+filename)
draw = ImageDraw.Draw(im)
font = draw.getfont()
size = font.getsize(filename)
draw.text((int((im.size[0]-size[0])/2),int((im.size[1]-size[1])/2)), filename, font=font, fill=(255,255,255,128))
im.save(folder+"watermarked/"+filename)
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment