#python #python-imaging-library
#Python #Python-imaging-library
Вопрос:
Я пытаюсь добавить обводку в этот создатель мемов на python. Этот код генерирует мем, но не добавляет к нему обводку. Как это сделать? Когда я использую его, в результирующем изображении появляется только белый текст. Я тоже хочу черную рамку для текста.
Вот мой код:
from PIL import Image, ImageDraw, ImageFont
import textwrap
def generate_meme(image_path, top_text, bottom_text='', font_path='./fonts/impact/impact.ttf', font_size=9):
# load image
im = Image.open(image_path)
draw = ImageDraw.Draw(im)
image_width, image_height = im.size
# load font
font = ImageFont.truetype(font=font_path, size=int(image_height*font_size)//100)
# convert text to uppercase
top_text = top_text.upper()
bottom_text = bottom_text.upper()
# text wrapping
char_width, char_height = font.getsize('A')
chars_per_line = image_width // char_width
top_lines = textwrap.wrap(top_text, width=chars_per_line)
bottom_lines = textwrap.wrap(bottom_text, width=chars_per_line)
# draw top lines
y = 10
for line in top_lines:
line_width, line_height = font.getsize(line)
x = (image_width - line_width)/2
draw.text((x,y), line, fill='white', font=font)
y = line_height
# draw bottom lines
y = image_height - char_height * len(bottom_lines) - 15
for line in bottom_lines:
line_width, line_height = font.getsize(line)
x = (image_width - line_width)/2
draw.text((x,y), line, fill='white', font=font)
y = line_height
# save meme
im.save('meme-' im.filename.split('/')[-1])
Ответ №1:
Это очень просто! Добавьте черную рамку перед добавлением белого текста Вот код
def generate_meme(image_path, top_text, bottom_text='', font_path='fonts/impact.ttf', font_size=9):
im = Image.open(image_path)
draw = ImageDraw.Draw(im)
image_width, image_height = im.size
font = ImageFont.truetype(font=font_path, size=int(image_height * font_size) // 100)
top_text = top_text.upper()
bottom_text = bottom_text.upper()
char_width, char_height = font.getsize('A')
chars_per_line = image_width // char_width
top_lines = textwrap.wrap(top_text, width=chars_per_line)
bottom_lines = textwrap.wrap(bottom_text, width=chars_per_line)
y = 10
for line in top_lines:
line_width, line_height = font.getsize(line)
x = (image_width - line_width) / 2
draw.text((x-1, y), line, font=font, fill='black')
draw.text((x 1, y), line, font=font, fill='black')
draw.text((x, y-1), line, font=font, fill='black')
draw.text((x, y 1), line, font=font, fill='black')
draw.text((x, y), line, fill='white', font=font)
y = line_height
y = image_height - char_height * len(bottom_lines) - 15
for line in bottom_lines:
line_width, line_height = font.getsize(line)
x = (image_width - line_width) / 2
draw.text((x-1, y), line, font=font, fill='black')
draw.text((x 1, y), line, font=font, fill='black')
draw.text((x, y-1), line, font=font, fill='black')
draw.text((x, y 1), line, font=font, fill='black')
draw.text((x, y), line, fill='white', font=font)
y = line_height
file_name = "memeimg.png"
ok = sedpath "/" file_name
im.save(ok, "PNG")