Skip to content

Commit c2afd5a

Browse files
authored
Update logo.py
1 parent 24b22b9 commit c2afd5a

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

plugins/logo.py

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,36 @@ async def logo_func(event):
2828
font_ = await temp.download_media("resources/fonts/")
2929
elif "pic" in mediainfo(temp.media):
3030
bg_ = await temp.download_media()
31-
if not bg_:
32-
if event.client._bot:
33-
SRCH = [
34-
"blur background",
35-
"background",
36-
"neon lights",
37-
"nature",
38-
"abstract",
39-
"space",
40-
"3d render",
41-
]
42-
with rm.get("search", helper=True, dispose=True) as mod:
43-
res = await mod.unsplash(random.choice(SRCH), limit=1)
44-
bg_, _ = await download_file(res[0], "resources/downloads/logo.png")
45-
newimg = "resources/downloads/unsplash-temp.jpg"
46-
img_ = Image.open(bg_)
47-
img_.resize((5000, 5000)).save(newimg)
48-
os.remove(bg_)
49-
bg_ = newimg
50-
else:
51-
pics = []
52-
async for i in event.client.iter_messages(
53-
"@UltroidLogos", filter=InputMessagesFilterPhotos
54-
):
55-
pics.append(i)
56-
id_ = random.choice(pics)
57-
bg_ = await id_.download_media()
31+
SRCH = [
32+
"background",
33+
"neon",
34+
"anime",
35+
"art",
36+
"bridges",
37+
"streets",
38+
"computer",
39+
"cyberpunk",
40+
"nature",
41+
"abstract",
42+
"exoplanet",
43+
"magic",
44+
"3d render",
45+
]
46+
with rm.get("search", helper=True, dispose=True) as mod:
47+
res = await mod.unsplash(random.choice(SRCH), limit=1)
48+
bg_, _ = await download_file(res[0], "resources/downloads/logo.png")
49+
newimg = "resources/downloads/unsplash-temp.jpg"
50+
img_ = Image.open(bg_)
51+
img_.resize((5000, 5000)).save(newimg)
52+
os.remove(bg_)
53+
bg_ = newimg
5854

5955
if not font_:
6056
fpath_ = glob.glob("resources/fonts/*")
6157
if not bool(fpath_):
62-
return await xx.edit("Add fonts in `resourses/fonts/` directory before using this command.")
58+
return await xx.edit(
59+
"Add fonts in `resourses/fonts/` directory before using this command."
60+
)
6361
font_ = random.choice(fpath_)
6462
if len(name) <= 8:
6563
strke = 10
@@ -88,15 +86,23 @@ async def logo_func(event):
8886
os.remove(bg_)
8987

9088

89+
90+
91+
9192
class LogoHelper:
93+
@staticmethod
94+
def get_text_size(text, image, font):
95+
im = Image.new("RGB", (image.width, image.height))
96+
draw = ImageDraw.Draw(im)
97+
return draw.textlength(text, font)
98+
9299
@staticmethod
93100
def find_font_size(text, font, image, target_width_ratio):
94101
tested_font_size = 100
95102
tested_font = ImageFont.truetype(font, tested_font_size)
96-
observed_width, observed_height = tested_font.getsize(text)
103+
observed_width = LogoHelper.get_text_size(text, image, tested_font)
97104
estimated_font_size = (
98-
tested_font_size / (observed_width /
99-
image.width) * target_width_ratio
105+
tested_font_size / (observed_width / image.width) * target_width_ratio
100106
)
101107
return round(estimated_font_size)
102108

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

110116
img = Image.open(imgpath)
111117
width, height = img.size
118+
fct = min(height, width)
119+
if height != width:
120+
img = img.crop((0, 0, fct, fct))
121+
if img.height < 1000:
122+
img = img.resize((1020, 1020))
123+
width, height = img.size
112124
draw = ImageDraw.Draw(img)
113125
font_size = LogoHelper.find_font_size(text, funt, img, width_ratio)
114126
font = ImageFont.truetype(funt, font_size)
115-
w, h = font.getsize(text)
127+
l, t, r, b = font.getbbox(text)
128+
w, h = r - l, (b - t) * 1.5
116129
draw.text(
117-
((width - w) / 2, (height - h) / 2),
130+
((width - w) / 2, ((height - h) / 2)),
118131
text,
119132
font=font,
120133
fill=fill,

0 commit comments

Comments
 (0)