简体   繁体   English

使用python,如何预测SVG中文本的像素宽度?

[英]using python, how do I predict the pixel width of text in an SVG?

I am using python to generate an SVG with text that can vary, and I'd like to put a translucent rectangle behind it to guarantee some contrast when it is overlayed on another graphical element later on. 我正在使用python来生成一个带有可以变化的文本的SVG,并且我想在它后面放一个半透明的矩形以保证稍后在其他图形元素上重叠时的一些对比度。

Unfortunately, I do not know how to predict the pixel extents of a specific string of text in SVG. 不幸的是,我不知道如何预测SVG中特定文本字符串的像素范围。 I can choose what font I use (but I don't think monospace will be acceptable) and what font size I'm using. 我可以选择我使用的字体(但我不认为monospace是可以接受的)以及我正在使用的字体大小。

How would I go about using python to predict the extents of the SVG text so I can generate an appropriately sized rectangle? 我将如何使用python预测SVG文本的范围,以便生成适当大小的矩形?

I think things would get messy if you did this in Python. 我觉得如果你用Python做这件事会让事情变得混乱。 Probably the least stressful approach would be to install FreeType and use it to calculate the text dimensions for you. 可能压力最小的方法可能是安装FreeType并使用它来为您计算文本尺寸。 But that's a very large hammer for quite a small nut. 但对于相当小的坚果来说,这是一个非常大的锤子。

This answer shows an easier approach — use an SVG filter to apply a background directly to the text without having to resize anything. 这个答案显示了一种更简单的方法 - 使用SVG过滤器将背景直接应用于文本,而无需调整任何大小。

You can use this method with translucent backgrounds too: 您也可以将此方法用于半透明背景:

 <svg width="360" height="100" viewBox="0 0 360 100"> <defs> <filter x="0" y="0" width="1" height="1" id="text-bg"> <feFlood flood-color="#ff0" flood-opacity="0.5" /> <feComposite in="SourceGraphic" /> </filter> </defs> <rect x="0" y="0" width="400" height="100" fill="#444" stroke="none" /> <path d="m0 10 40 80 40-80 40 80 40-80 40 80 40-80 40 80 40-80 40 80" stroke="#888" stroke-width="10" fill="none" /> <text font-family="Arial" font-weight="bold" filter="url(#text-bg)" x="180" y="50" font-size="20" fill="#000" text-anchor="middle"> Edit this text and reload the SVG </text> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在svg文件中可编辑的python图中保存文本? - How do I save the text in python plots editable in svg files? 如何使用 PyTorch model 进行预测? - How do I predict using a PyTorch model? 如何使用python逐像素显示图像? - How can I show an image plotting pixel by pixel using python? 如何使用Python获取SVG路径的高度,宽度和长度? - How can I get the height and width and length of an SVG path using Python? 如何使用 RandomForestRegressor 方法在 Python 中使用 scikitlearn、pandas 预测未来结果? - How do I predict future results with scikitlearn, pandas in Python using RandomForestRegressor method? 如何预测 Python 随机数生成器的输出? - How do I predict the output of Python's random number generator? 如何正确预测 python 中的近期值? - How do I predict the near future value correctly in python? 我如何使用 python sklearn 线性回归预测数据 - How do i predict data with python sklearn linear regression 我想预测新事件的更新数量吗? 如何在python中做到这一点? - I want to predict the no.of updates for a new incident? how to do that in python? 如何使用 kernel 返回像素值数组以压缩它们(模糊)? *Python* - How do I return an array of pixel values using kernel to condense them (blur)? *Python*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM