用于将文本创建为位图的工具(抗锯齿文本、自定义间距、透明背景)

发布于 2024-07-11 21:29:18 字数 1028 浏览 7 评论 0原文

我需要批量创建带有文本的图像。 要求:

  1. 任意大小的位图
  2. PNG 格式
  3. 透明背景
  4. 黑色文本 抗锯齿透明度
  5. 可调字符间距
  6. 可调文本位置(文本开始的 x 和 y 坐标)
  7. TrueType 和/或 Type1 支持
  8. Unix 命令行工具或 Python 库

到目前为止,我已经评估了以下内容:

PIL 的问题在于,例如Verdana 太稀疏了。 我需要文本更紧凑一些,但是在 PIL 中没有办法调整它。

在 ImageMagick 中,我还没有找到一种简单的方法来指定图像中文本的开始位置(我使用 -size WIDTHxHEIGHT 和标题:'TEXT')。 添加透明边框会将文本移离它所固定的角,但

  • 图像大小需要相应调整,因为边框会增加一定范围,
  • 无法独立调整水平和垂直偏移

我是否错过了一些明显的替代方案或未能找到上面提到的必要功能?

I need to batch create images with text. Requirements:

  1. arbitrary size of bitmap
  2. PNG format
  3. transparent background
  4. black text anti-aliased against transparency
  5. adjustable character spacing
  6. adjustable text position (x and y coordinates where text begins)
  7. TrueType and/or Type1 support
  8. Unix command line tool or Python library

So far I've evaluated the following:

The problem with PIL is that e.g. the default spacing for Verdana is way too sparse. I need the text to be a bit tighter, but there's no way to adjust it in PIL.

In ImageMagick I haven't found an easy way to specify where in the image the text begins (I'm using -size WIDTHxHEIGHT and caption:'TEXT'). Adding a transparent border will move the text away from the corner it's achored to, but

  • image size needs to be adjusted accordingly since border adds to the extents
  • it's not possible to adjust horizontal and vertical offset independently

Have I missed some obvious alternatives or failed to find necessary features from the above mentioned?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔书几行 2024-07-18 21:29:18

(5) 确实看起来很棘手,缺少在字符串中插入虚拟窄空格(这会破坏字距调整)或使用更高级别的东西,如 SVG 或 HTML/CSS 渲染器。

然而,如果您不介意亲自动手,那么破解 PIL 的 freetype 渲染器来添加水平空间看起来很容易。 请参阅_imagingft.c; 在 font_getsize 和 font_render 中添加以下代码之后:

if (kerning && last_index && index) {
    FT_Vector delta;
    FT_Get_Kerning(self->face, last_index, index, ft_kerning_default,
                   &delta);
    x += delta.x >> 6;
}

添加:

if (last_index && index) {
    x += tracking;
}

首先尝试使用普通整数进行跟踪(根据“>> 6”判断可能相当大); 编译看看是否有效。 下一步是将跟踪值从 Python 获取到 C 函数中,为此您必须将 font_render 中的 ParseTuple 调用更改为:

long tracking;
if (!PyArg_ParseTuple(args, "Ol|il:render", &string, &id, &mask, &tracking))
    return NULL;

并在 font_getsize 中:

long tracking;
if (!PyArg_ParseTuple(args, "O|l:getsize", &string, &tracking))
    return NULL;

然后查看您想要的 Python 接口。 这是一个微不足道但相当乏味的案例,通过界面的每个级别添加额外的“跟踪”参数,例如:

def truetype(filename, size, index=0, encoding="", tracking= 0): # added optional tracking
    "Load a truetype font file."
    try:
        return FreeTypeFont(filename, size, index, encoding, tracking) # added tracking
    ...

class FreeTypeFont:
    "FreeType font wrapper (requires _imagingft service)"

    def __init__(self, file, size, index=0, encoding="", tracking= 0): # added tracking
        import _imagingft
        self.font = _imagingft.getfont(file, size, index, encoding)
        self.tracking= tracking # add this line

    ...

    def getmask2(self, text, mode="", fill=Image.core.fill):
        size, offset = self.font.getsize(text, self.tracking) # use tracking
        im = fill("L", size, 0)
        self.font.render(text, im.id, mode=="1", self.tracking) # use tracking
        return im, offset

我还没有测试过这些! 如果它有效,可能值得作为补丁提交。

(5) indeed looks tricky, short of inserting dummy narrow-spaces into the string (which will break kerning) or using something much higher-level like the SVG or HTML/CSS renderer.

However, if you don't mind getting your hands dirty, it looks quite easy to hack PIL's freetype renderer into adding horizontal space. See _imagingft.c; after the following code in both font_getsize and font_render:

if (kerning && last_index && index) {
    FT_Vector delta;
    FT_Get_Kerning(self->face, last_index, index, ft_kerning_default,
                   &delta);
    x += delta.x >> 6;
}

Add:

if (last_index && index) {
    x += tracking;
}

Try it with a plain integer for tracking (probably quite large judging by that '>>6') first; compile and see if it works. The next step would be to get the tracking value into the C function from Python, for which you would have to change the ParseTuple call in font_render to:

long tracking;
if (!PyArg_ParseTuple(args, "Ol|il:render", &string, &id, &mask, &tracking))
    return NULL;

And in font_getsize:

long tracking;
if (!PyArg_ParseTuple(args, "O|l:getsize", &string, &tracking))
    return NULL;

Then look at what Python interface you want. This is a trivial but quite tedious case of adding the extra 'tracking' argument through each level of the interface, for example:

def truetype(filename, size, index=0, encoding="", tracking= 0): # added optional tracking
    "Load a truetype font file."
    try:
        return FreeTypeFont(filename, size, index, encoding, tracking) # added tracking
    ...

class FreeTypeFont:
    "FreeType font wrapper (requires _imagingft service)"

    def __init__(self, file, size, index=0, encoding="", tracking= 0): # added tracking
        import _imagingft
        self.font = _imagingft.getfont(file, size, index, encoding)
        self.tracking= tracking # add this line

    ...

    def getmask2(self, text, mode="", fill=Image.core.fill):
        size, offset = self.font.getsize(text, self.tracking) # use tracking
        im = fill("L", size, 0)
        self.font.render(text, im.id, mode=="1", self.tracking) # use tracking
        return im, offset

I haven't tested any of this! If it works, might be worth submitting as a patch.

短暂陪伴 2024-07-18 21:29:18

以下是 SVG + ImageMagick 解决方案:

基于此模板以编程方式创建 SVG 文档,将“TEXT HERE”替换为所需的文本内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC
      "-//W3C//DTD SVG 1.0//EN"
      "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" width="152px" height="50px">
  <text style="font-size: 22px; font-weight:bold; font-family: Verdana-Bold;
               letter-spacing: -1.3%;">
    <tspan x="10" y="39">TEXT HERE</tspan>
  </text>
</svg>

使用 ImageMagick 的 convert 将文档转换为背景透明的 PNG:

$ convert -background none input.svg output.png

Here's the SVG + ImageMagick solution:

Programmatically create SVG documents based on this template, replacing "TEXT HERE" with the desired text content:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC
      "-//W3C//DTD SVG 1.0//EN"
      "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" width="152px" height="50px">
  <text style="font-size: 22px; font-weight:bold; font-family: Verdana-Bold;
               letter-spacing: -1.3%;">
    <tspan x="10" y="39">TEXT HERE</tspan>
  </text>
</svg>

Convert the documents to background-transparent PNGs with ImageMagick's convert:

$ convert -background none input.svg output.png
心的位置 2024-07-18 21:29:18

快速浏览一下,Pango 支持 字母间距。 Pango 具有 Python 绑定并与 Cairo 集成。

From a quick glance, Pango has support for letter spacing. Pango has Python bindings and is integrated with Cairo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文