Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os, math, sys
  4. from PIL import Image, ImageFont, ImageDraw
  5.  
  6. wm_width = 1254
  7. wm_height = 705
  8. brightness_threshold = 125
  9.  
  10. def determine_brightness(image):
  11. gray = image.convert('L')
  12. top_left = ((gray.width - wm_width) // 2, (gray.height - wm_height) // 2)
  13.  
  14. total = 0
  15. for i in range(top_left[0], top_left[0] + wm_width):
  16. for j in range(top_left[1], top_left[1] + wm_height):
  17. total += gray.getpixel((i, j))
  18. brightness = total / (wm_width * wm_height) # This is on a scale of 0-256
  19. return brightness
  20.  
  21.  
  22. def watermark():
  23. with Image.open("IMG_0035.png") as image:
  24. if determine_brightness(image) > brightness_threshold:
  25. black = Image.open("./{}.png".format("black-t")).convert("RGBA")
  26. logo = black
  27. else:
  28. white = Image.open("./{}.png".format("white-t")).convert("RGBA")
  29. logo = white
  30.  
  31.  
  32. image_w, image_h = image.size
  33.  
  34. logo_w, logo_h = logo.size
  35. offset = ((image_w - logo_w) // 2, (image_h - logo_h) // 2)
  36.  
  37. image_copy = image.copy()
  38. position = ((image_copy.width - logo.width), (image_copy.height - logo.height))
  39.  
  40. image_copy.paste(logo, offset, logo)
  41. image_copy.show()
  42.  
  43.  
  44. watermark()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement