objektify2 / maker.py
tripleS-Dev
update V1.3.8
5ad9ad5
raw
history blame contribute delete
No virus
2.04 kB
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)