Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created August 2, 2021 11:57
Show Gist options
  • Save bradmartin333/f342af2025e1d9e6974518e3b282ff02 to your computer and use it in GitHub Desktop.
Save bradmartin333/f342af2025e1d9e6974518e3b282ff02 to your computer and use it in GitHub Desktop.
Watermarker
from PIL import Image, ImageFont, ImageDraw, ExifTags
import pathlib
import os
imageExtensions = ['.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif']
basewidth = 600 # Adjust output file size
# Make output dir
if not os.path.exists('watermark'):
os.makedirs('watermark')
for entry in os.scandir(os.getcwd()):
path = pathlib.PurePath(entry)
extention = path.suffix
if extention in imageExtensions:
img = Image.open(entry.path)
# Match EXIF rotattion
for orientation in ExifTags.TAGS.keys() :
if ExifTags.TAGS[orientation]=='Orientation' : break
exif=dict(img._getexif().items())
if exif[orientation] == 3 :
img = img.rotate(180, expand=True)
elif exif[orientation] == 6 :
img = img.rotate(270, expand=True)
elif exif[orientation] == 8 :
img= img.rotate(90, expand=True)
# Show image for reference
os.startfile(path, 'open')
thisStr = input(path.name + " : ")
# Resize and preserve aspect ratio
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
draw = ImageDraw.Draw(img)
# Draw text with background
font = ImageFont.truetype('lucon.ttf', 50)
w, h = font.getsize(thisStr)
draw.rectangle((0, 0, w, h), fill='black')
draw.text((0,0),thisStr,font=font,fill=(255,255,255))
# Save image
img.save(str(os.getcwd()) + '/watermark/' + thisStr + extention)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment