Spaces:
Running
Running
import asyncio | |
from PIL import ImageFont, ImageDraw, Image | |
async def get_text_size(text, font_path, font_size, variation = None): | |
# ํฐํธ๋ฅผ ๋ก๋ํฉ๋๋ค. | |
font = ImageFont.truetype(font_path, font_size) | |
if variation: | |
font.set_variation_by_name(variation) | |
# ์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํฉ๋๋ค. | |
dummy_image = Image.new('RGB', (1, 1)) | |
draw = ImageDraw.Draw(dummy_image) | |
# ํ ์คํธ์ ํฌ๊ธฐ๋ฅผ ๊ณ์ฐํฉ๋๋ค. | |
text_bbox = draw.textbbox((0, 0), text, font=font) | |
text_size = (text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]) | |
return text_size | |
async def draw(font_size, text, color= (211, 213, 216), font_path = "75B.otf", variation = None): | |
font = ImageFont.truetype(font_path, font_size) | |
if variation: | |
font.set_variation_by_name(variation) | |
if font_path == 'inter.ttf': | |
offset_y = 10 | |
else: | |
offset_y = 15 | |
x, y = await get_text_size(text, font_path, font_size, variation) | |
image = Image.new("RGBA", (x, y+offset_y), (0, 0, 0, 0)) | |
draw = ImageDraw.Draw(image) | |
draw.text((0, 0), text, fill=color, font=font) | |
return image | |
def space(base_img, box_position, box_size, objekt_img): | |
# ๊ฐ์ ์์ ์์น์ ํฌ๊ธฐ | |
x2, y2 = box_position | |
x3, y3 = box_size | |
# ๊ฐ์ฒด ์ด๋ฏธ์ง ํฌ๊ธฐ ๊ฐ์ ธ์ค๊ธฐ | |
x4, y4 = objekt_img.size | |
# ์ค์ ์์น ๊ณ์ฐ | |
xc = x2 + (x3 - x4) / 2 | |
yc = y2 + (y3 - y4) / 2 | |
# ๊ฐ์ฒด ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ์์์ ์ค์์ ๋ถ์ด๊ธฐ | |
base_img.paste(objekt_img, (int(xc), int(yc)), objekt_img) | |
return base_img | |
async def run(): | |
(await draw(40, 'Binary01', font_path='inter.ttf', variation='Regular')).show() | |
#asyncio.run(run()) | |
#font = ImageFont.truetype('HalvarBreit-XBd.ttf').get_variation_names() | |
#print(font) |