On Linux your code gives me 700
but I don't see Consolas
on list tkFont.families()
.
But I get your expected result with pillow.ImageFont
(but it needs full path to Consolas
)
from PIL import ImageFont
text = 'If you have installed Selenium Python bindings, you can start using it'
font = ImageFont.truetype('/full/path/to/Consolas.ttf', 17)
print('getbbox :', font.getbbox(text))
print('getlength:', font.getlength(text))
print('getsize :', font.getsize(text))
Result:
getbbox : (0, 1, 654, 16) # (x, y, width, height)
getlength: 654.0625
getsize : (654, 16)
Doc: ImageFont
EDIT:
You can use pillow
to generate image with text and compare it with text on screenshot.
from PIL import Image, ImageFont, ImageDraw
data = [
('If you have installed Selenium Python bindings, you can start using it', 17),
('This sample analysis model is expected to be contributing to students', 17),
('conduct academic research and studies, for teachers of English while selecting', 17),
]
for number, (text, size) in enumerate(data, 1):
font = ImageFont.truetype('/full/path/to/Consolas.ttf', size)
print('text :', text)
print('font size:', size)
print('getbbox :', font.getbbox(text))
print('getlength:', font.getlength(text))
print('getsize :', font.getsize(text))
print('----')
image = Image.new('RGB', font.getsize(text))
draw = ImageDraw.Draw(image)
draw.text((0,0), text, font=font)
image.save(f'text-{number}-size-{size}.png')
Result:
text : If you have installed Selenium Python bindings, you can start using it
font size: 17
getbbox : (0, 1, 654, 16)
getlength: 654.0625
getsize : (654, 16)
----
text : This sample analysis model is expected to be contributing to students
font size: 17
getbbox : (0, 1, 645, 16)
getlength: 644.71875
getsize : (645, 16)
----
text : conduct academic research and studies, for teachers of English while selecting
font size: 17
getbbox : (0, 1, 729, 16)
getlength: 728.8125
getsize : (729, 16)
----
text-1-size-17.png

text-2-size-17.png

text-3-size-17.png
