Skip to content

Commit

Permalink
Update logo.py
Browse files Browse the repository at this point in the history
  • Loading branch information
New-dev0 committed May 17, 2024
1 parent 24b22b9 commit c2afd5a
Showing 1 changed file with 46 additions and 33 deletions.
79 changes: 46 additions & 33 deletions plugins/logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,36 @@ async def logo_func(event):
font_ = await temp.download_media("resources/fonts/")
elif "pic" in mediainfo(temp.media):
bg_ = await temp.download_media()
if not bg_:
if event.client._bot:
SRCH = [
"blur background",
"background",
"neon lights",
"nature",
"abstract",
"space",
"3d render",
]
with rm.get("search", helper=True, dispose=True) as mod:
res = await mod.unsplash(random.choice(SRCH), limit=1)
bg_, _ = await download_file(res[0], "resources/downloads/logo.png")
newimg = "resources/downloads/unsplash-temp.jpg"
img_ = Image.open(bg_)
img_.resize((5000, 5000)).save(newimg)
os.remove(bg_)
bg_ = newimg
else:
pics = []
async for i in event.client.iter_messages(
"@UltroidLogos", filter=InputMessagesFilterPhotos
):
pics.append(i)
id_ = random.choice(pics)
bg_ = await id_.download_media()
SRCH = [
"background",
"neon",
"anime",
"art",
"bridges",
"streets",
"computer",
"cyberpunk",
"nature",
"abstract",
"exoplanet",
"magic",
"3d render",
]
with rm.get("search", helper=True, dispose=True) as mod:
res = await mod.unsplash(random.choice(SRCH), limit=1)
bg_, _ = await download_file(res[0], "resources/downloads/logo.png")
newimg = "resources/downloads/unsplash-temp.jpg"
img_ = Image.open(bg_)
img_.resize((5000, 5000)).save(newimg)
os.remove(bg_)
bg_ = newimg

if not font_:
fpath_ = glob.glob("resources/fonts/*")
if not bool(fpath_):
return await xx.edit("Add fonts in `resourses/fonts/` directory before using this command.")
return await xx.edit(
"Add fonts in `resourses/fonts/` directory before using this command."
)
font_ = random.choice(fpath_)
if len(name) <= 8:
strke = 10
Expand Down Expand Up @@ -88,15 +86,23 @@ async def logo_func(event):
os.remove(bg_)





class LogoHelper:
@staticmethod
def get_text_size(text, image, font):
im = Image.new("RGB", (image.width, image.height))
draw = ImageDraw.Draw(im)
return draw.textlength(text, font)

@staticmethod
def find_font_size(text, font, image, target_width_ratio):
tested_font_size = 100
tested_font = ImageFont.truetype(font, tested_font_size)
observed_width, observed_height = tested_font.getsize(text)
observed_width = LogoHelper.get_text_size(text, image, tested_font)
estimated_font_size = (
tested_font_size / (observed_width /
image.width) * target_width_ratio
tested_font_size / (observed_width / image.width) * target_width_ratio
)
return round(estimated_font_size)

Expand All @@ -109,12 +115,19 @@ def make_logo(imgpath, text, funt, **args):

img = Image.open(imgpath)
width, height = img.size
fct = min(height, width)
if height != width:
img = img.crop((0, 0, fct, fct))
if img.height < 1000:
img = img.resize((1020, 1020))
width, height = img.size
draw = ImageDraw.Draw(img)
font_size = LogoHelper.find_font_size(text, funt, img, width_ratio)
font = ImageFont.truetype(funt, font_size)
w, h = font.getsize(text)
l, t, r, b = font.getbbox(text)
w, h = r - l, (b - t) * 1.5
draw.text(
((width - w) / 2, (height - h) / 2),
((width - w) / 2, ((height - h) / 2)),
text,
font=font,
fill=fill,
Expand Down

0 comments on commit c2afd5a

Please sign in to comment.