Sample code membuat welcome image menggunakan python dan pillow. Code ini saya buat seadanya terinspirasi dari bot discord 🤕. Kode ini bisa dites dengan eval asal sudah mendownload font tersebut
import os
from PIL import Image, ImageChops, ImageDraw, ImageFont
import textwrap
def circle(pfp, size=(215, 215)):
pfp = pfp.resize(size, Image.ANTIALIAS).convert("RGBA")
bigsize = (pfp.size[0] * 3, pfp.size[1] * 3)
mask = Image.new('L', bigsize, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + bigsize, fill=255)
mask = mask.resize(pfp.size, Image.ANTIALIAS)
mask = ImageChops.darker(mask, pfp.split()[-1])
pfp.putalpha(mask)
return pfp
def draw_multiple_line_text(image, text, font, text_start_height):
draw = ImageDraw.Draw(image)
image_width, image_height = image.size
y_text = text_start_height
lines = textwrap.wrap(text, width=38)
for line in lines:
line_width, line_height = font.getsize(line)
draw.text(((image_width - line_width) / 2, y_text),
line,
font=font,
fill="black")
y_text += line_height
async def main():
filename = "result.png"
background = Image.open("/YasirBot/img/bg.png")
background = background.resize((1024, 500), Image.ANTIALIAS)
pic = await app.download_media(message.reply_to_message.from_user.photo.big_file_id)
pfp = Image.open(pic).convert("RGBA")
pfp = circle(pfp)
pfp = pfp.resize(
(265,
265))
font = ImageFont.truetype(
"/YasirBot/LemonMilkMedium-mLZYV.otf", 42
)
member_text = (f"Selamat Datang {message.reply_to_message.from_user.first_name}"
)
draw_multiple_line_text(background, member_text, font, 385)
draw_multiple_line_text(background,
message.chat.title, font,
23)
background.paste(pfp, (379, 123),
pfp)
background.save(
filename)
await main()
await message.reply_photo("result.png")
try:
os.remove(pic)
os.remove("result.png")
except:
pass
import os
from PIL import Image, ImageChops, ImageDraw, ImageFont
import textwrap
def circle(pfp, size=(215, 215)):
pfp = pfp.resize(size, Image.ANTIALIAS).convert("RGBA")
bigsize = (pfp.size[0] * 3, pfp.size[1] * 3)
mask = Image.new('L', bigsize, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + bigsize, fill=255)
mask = mask.resize(pfp.size, Image.ANTIALIAS)
mask = ImageChops.darker(mask, pfp.split()[-1])
pfp.putalpha(mask)
return pfp
def draw_multiple_line_text(image, text, font, text_start_height):
draw = ImageDraw.Draw(image)
image_width, image_height = image.size
y_text = text_start_height
lines = textwrap.wrap(text, width=38)
for line in lines:
line_width, line_height = font.getsize(line)
draw.text(((image_width - line_width) / 2, y_text),
line,
font=font,
fill="black")
y_text += line_height
async def main():
filename = "result.png"
background = Image.open("/YasirBot/img/bg.png")
background = background.resize((1024, 500), Image.ANTIALIAS)
pic = await app.download_media(message.reply_to_message.from_user.photo.big_file_id)
pfp = Image.open(pic).convert("RGBA")
pfp = circle(pfp)
pfp = pfp.resize(
(265,
265))
font = ImageFont.truetype(
"/YasirBot/LemonMilkMedium-mLZYV.otf", 42
)
member_text = (f"Selamat Datang {message.reply_to_message.from_user.first_name}"
)
draw_multiple_line_text(background, member_text, font, 385)
draw_multiple_line_text(background,
message.chat.title, font,
23)
background.paste(pfp, (379, 123),
pfp)
background.save(
filename)
await main()
await message.reply_photo("result.png")
try:
os.remove(pic)
os.remove("result.png")
except:
pass