Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import os, math, sys
- from PIL import Image, ImageFont, ImageDraw
- wm_width = 1254
- wm_height = 705
- brightness_threshold = 125
- def determine_brightness(image):
- gray = image.convert('L')
- top_left = ((gray.width - wm_width) // 2, (gray.height - wm_height) // 2)
- total = 0
- for i in range(top_left[0], top_left[0] + wm_width):
- for j in range(top_left[1], top_left[1] + wm_height):
- total += gray.getpixel((i, j))
- brightness = total / (wm_width * wm_height) # This is on a scale of 0-256
- return brightness
- def watermark():
- with Image.open("IMG_0035.png") as image:
- if determine_brightness(image) > brightness_threshold:
- black = Image.open("./{}.png".format("black-t")).convert("RGBA")
- logo = black
- else:
- white = Image.open("./{}.png".format("white-t")).convert("RGBA")
- logo = white
- image_w, image_h = image.size
- logo_w, logo_h = logo.size
- offset = ((image_w - logo_w) // 2, (image_h - logo_h) // 2)
- image_copy = image.copy()
- position = ((image_copy.width - logo.width), (image_copy.height - logo.height))
- image_copy.paste(logo, offset, logo)
- image_copy.show()
- watermark()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement