J T*_*J T 3 python imagemagick wand
I need to calculate the length when rendered of a lot of strings using a custom font. From a shell script and ImageMagick I am able to do something using the annotate command line option.
convert -debug annotate xc: -font "customfont.ttf" -pointsize "25" -annotate 0 "this is the text" out.png
Run Code Online (Sandbox Code Playgroud)
And then reading the width of the rendered image.
Im struggling to understand how to do this with the python 'Wand' lib. I have created a font object, but I seem to need to define the width of the canvas to paint the font onto.
Any suggestions appreciated.
使用魔杖,您将使用wand.drawing.Drawing.get_font_metricswhich 将返回FontMetrics类的实例。
例子
from wand.image import Image
from wand.drawing import Drawing
with Image(filename='wizard:') as img:
with Drawing() as context:
context.font_family = 'monospace'
context.font_size = 25
metrics = context.get_font_metrics(img,
"How BIG am I?",
multiline=False)
print(metrics)
#=> FontMetrics(character_width=25.0,
# character_height=25.0,
# ascender=23.0,
# descender=-5.0,
# text_width=170.0,
# text_height=29.0,
# maximum_horizontal_advance=50.0,
# x1=0.0,
# y1=0.0,
# x2=19.21875,
# y2=18.0,
# x=170.0,
# y=0.0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1606 次 |
| 最近记录: |