728x90
반응형
Font Loading 후, font와 관련된 요소 추출 시, 참고
+) PIL이 update 되면서 text 자체의 box를 Fit하게 가져오는 코드가 생김
text_bbox = draw.textbbox((0, 0), text, font=font)
draw.rectangle(text_bbox, outline=(0, 150, 150), width=2)#CYAN color
+) 특수문자는 예외..
def font_inner_bbox():
im = Image.new('RGB', (700, 400), (200, 200, 200))
text = 'AQj'
font = ImageFont.truetype('./resources/font/handwriting_font/FONT.ttf', size=220)
ascent, descent = font.getmetrics()
print(ascent, descent)
# font
# ascent (the distance from the baseline to the highest outline point)
# descent (the distance from the baseline to the lowest outline point, a negative value)
(width, height), (offset_x, offset_y) = font.font.getsize(text)
draw = ImageDraw.Draw(im)
draw.rectangle([(0, 0), (width, offset_y)], fill=(237, 127, 130)) # Red
draw.rectangle([(0, offset_y), (width, ascent)], fill=(202, 229, 134)) # Green
draw.rectangle([(0, ascent), (width, ascent + descent)], fill=(134, 190, 229)) # Blue
draw.rectangle(font.getmask(text).getbbox(), outline=(0, 0, 0)) # Black
draw.rectangle([(0, offset_y), (width, ascent + descent)], outline=(0, 255, 255)) # SKY
text_bbox = draw.textbbox((0, 0), text, font=font)
draw.rectangle(text_bbox, outline=(0, 150, 150), width=2)#CYAN color
draw.text((0, 0), text, font=font, fill=(0, 0, 0))
im.save('result.jpg')
print(width, height)
print(offset_x, offset_y)
print('Red height', offset_y)
print('Green height', ascent - offset_y)
print('Blue height', descent)
print('Black', font.getmask(text).getbbox())
728x90
반응형