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)