A bot that overlays the "you died" text on stuff
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

49 lignes
1.6 KiB

  1. from PIL import Image, ImageDraw, ImageFont
  2. def sub(p1, p2):
  3. return (p1[0] - p2[0], p1[1] - p2[1])
  4. def overlay(image_in, message="YOU DIED", color=(200, 25, 25)):
  5. image = image_in.convert("RGBA")
  6. size = image.size
  7. center = (size[0]/2, size[1]/2)
  8. limit = size[1]
  9. if size[0] / size[1] < 1:
  10. limit = int(limit * size[0] / size[1])
  11. font_size = limit // 5
  12. gradient_range = limit // 10
  13. rect_top = sub(center, (0, font_size * 3 // 5))
  14. rect_bot = sub(center, (0, -font_size * 3 // 5))
  15. overlay = Image.new("RGBA", size, (0,0,0,0))
  16. draw = ImageDraw.Draw(overlay)
  17. font = ImageFont.truetype("OptimusPrinceps.ttf", font_size)
  18. draw.rectangle([0, rect_top[1], size[0], rect_bot[1]], fill=(0,0,0, 225))
  19. for y in range(-1, -gradient_range - 1, -1):
  20. c = 225 - 225 * (-y) // gradient_range
  21. draw.line([0, rect_top[1] + y, size[0], rect_top[1] + y], fill=(0,0,0,c))
  22. for y in range(1, gradient_range + 1, 1):
  23. c = 225 - 225 * (y) // gradient_range
  24. draw.line([0, rect_bot[1] + y, size[0], rect_bot[1] + y], fill=(0,0,0,c))
  25. text_size = draw.textsize(message, font=font)
  26. # in case the text is too long
  27. ratio = text_size[0] / size[0]
  28. if ratio > 1:
  29. font = ImageFont.truetype("OptimusPrinceps.ttf", int(font_size / ratio))
  30. text_size = draw.textsize(message, font=font)
  31. offset = (center[0] - text_size[0]/2, center[1] - text_size[1]/2)
  32. draw.text(offset, message, color, font=font)
  33. result = Image.alpha_composite(Image.alpha_composite(image, Image.new("RGBA", size, (0,0,0,125))), overlay)
  34. return result