Explorar el Código

Allow for text to be provided in the caption

master
Fen Dweller hace 5 años
padre
commit
1c26dabad6
Se han modificado 2 ficheros con 17 adiciones y 4 borrados
  1. +9
    -2
      overlay.py
  2. +8
    -2
      you-died.py

+ 9
- 2
overlay.py Ver fichero

@@ -3,12 +3,11 @@ from PIL import Image, ImageDraw, ImageFont
def sub(p1, p2):
return (p1[0] - p2[0], p1[1] - p2[1])

def overlay(image_in):
def overlay(image_in, message="YOU DIED"):
image = image_in.convert("RGBA")

size = image.size
center = (size[0]/2, size[1]/2)
message = "YOU DIED"
limit = size[1]
if size[0] / size[1] < 1:
limit = int(limit * size[0] / size[1])
@@ -34,6 +33,14 @@ def overlay(image_in):
draw.line([0, rect_bot[1] + y, size[0], rect_bot[1] + y], fill=(0,0,0,c))

text_size = draw.textsize(message, font=font)

# in case the text is too long
ratio = text_size[0] / size[0]

if ratio > 1:
font = ImageFont.truetype("OptimusPrinceps.ttf", int(font_size / ratio))
text_size = draw.textsize(message, font=font)

offset = (center[0] - text_size[0]/2, center[1] - text_size[1]/2)

draw.text(offset, message, (200,25,25), font=font)


+ 8
- 2
you-died.py Ver fichero

@@ -13,10 +13,13 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s

def overlay_photo(update: Update, context: CallbackContext):
try:
caption = update.message.caption
if not caption:
caption = "YOU DIED"
file = context.bot.getFile(update.message.photo[-1].file_id)
bytes = file.download_as_bytearray()
image = Image.open(io.BytesIO(bytes))
overlayed = overlay(image).convert("RGB")
overlayed = overlay(image, caption).convert("RGB")
output = io.BytesIO()
overlayed.save(output, "JPEG")
output.seek(0)
@@ -26,10 +29,13 @@ def overlay_photo(update: Update, context: CallbackContext):

def overlay_document(update: Update, context: CallbackContext):
try:
caption = update.message.caption
if not caption:
caption = "YOU DIED"
file = context.bot.getFile(update.message.document.file_id)
bytes = file.download_as_bytearray()
image = Image.open(io.BytesIO(bytes))
overlayed = overlay(image)
overlayed = overlay(image, caption)
output = io.BytesIO()
overlayed.save(output, "PNG")
output.seek(0)


Cargando…
Cancelar
Guardar