적합성을 찾을 때까지 글꼴 크기를 증가시킬 수 있습니다. font.getsize()
은 렌더링 된 텍스트의 크기를 알려주는 함수입니다. 이 당신을 위해 충분히 효율적으로하지 않으면
import ImageFont, ImageDraw, Image
image = Image.open('hsvwheel.png')
draw = ImageDraw.Draw(image)
txt = "Hello World"
fontsize = 1 # starting font size
# portion of image width you want text width to be
img_fraction = 0.50
font = ImageFont.truetype("arial.ttf", fontsize)
while font.getsize(txt)[0] < img_fraction*image.size[0]:
# iterate until the text size is just larger than the criteria
fontsize += 1
font = ImageFont.truetype("arial.ttf", fontsize)
# optionally de-increment to be sure it is less than criteria
fontsize -= 1
font = ImageFont.truetype("arial.ttf", fontsize)
print 'final font size',fontsize
draw.text((10, 25), txt, font=font) # put the text on the image
image.save('hsvwheel_txt.png') # save it
, 당신은 루트 찾는 방식을 구현할 수 있습니다,하지만 난 font.getsize()
기능은 이미지 편집 과정의 나머지 부분에 비해 작은 감자입니다 같은데요.
안녕 폴, 그 트릭을했다. 도와 주셔서 감사합니다 – Shpongle