from PIL import ImageFont, ImageDraw, Image 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 def hex_to_rgb(hex_color): # Remove the hash at the start if it's there hex_color = hex_color.lstrip('#') # Convert the string into integer values, then into a tuple rgb_tuple = tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4)) return rgb_tuple def draw(font_size, text, color= (211, 213, 216), font_path = "./font/Helvetica_Neue_LT_Std_75_Bold.otf", variation = None): if isinstance(color, str): color = hex_to_rgb(color) 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 = 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 #asyncio.run(run()) #font = ImageFont.truetype('HalvarBreit-XBd.ttf').get_variation_names() #print(font)