Skip to content

Instantly share code, notes, and snippets.

@SirSaleh
Last active July 30, 2019 10:36
Show Gist options
  • Save SirSaleh/5d0e07ef6d1e606edd4dda6995f23613 to your computer and use it in GitHub Desktop.
Save SirSaleh/5d0e07ef6d1e606edd4dda6995f23613 to your computer and use it in GitHub Desktop.
Watermark a directory photos automatically using python and PIL (tested on python3)
import os
from PIL import Image
def set_watermark(photo, watermark):
photo_size = photo.size
watermark_size = watermark.size
print(watermark_size)
location = (0, int(photo_size[1]/1.3))
b = int(photo_size[1] / 10)
watermark = watermark.resize((int(watermark_size[0]/watermark_size[1]*b), b))
photo.paste(watermark, location, watermark)
file_address = photo.filename
filename = file_address[file_address.rfind("/")+1:]
# path to save watermarked photos
photo.save("/home/saleh/Desktop/water/water/"+filename)
def set_watermark_dir(path):
# photo extensions you want
# use uppercase forms if needed
included_extensions = ['jpg','jpeg', 'bmp', 'png', 'gif']
file_names = [fn for fn in os.listdir(path)
if any(fn.endswith(ext) for ext in included_extensions)]
# watermark photo adress
watermark = Image.open("/home/saleh/Desktop/water/watermark.png")
for file_name in file_names:
print(path+file_name)
photo = Image.open(path+"/"+file_name)
set_watermark(photo, watermark)
# call the function with path of photos dir source
set_watermark_dir("/home/saleh/Desktop/shirphoto")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment