【问题标题】:How do I measure the bounds of a string in wand?如何测量魔杖中字符串的边界?
【发布时间】:2018-10-02 15:30:52
【问题描述】:

我正在使用 Wand 生成包含自定义变量文本的 JPG。 我有一个字符串数组,它们都具有相同的宽度但不同的高度。 有没有一种方法可以在边界内将长文本换行或计算文本所需的高度,以便在从数组中绘制文本时它们不会重叠。

with Drawing() as ctx:
        with Image(width=1080, height=1080, background=Color("WHITE")) as img:
            with Drawing() as draw: 
                for i,line in enumerate(lines):
                     metrics = draw.get_font_metrics(img, line, multiline=True)
                     draw.text(x=150, y=120+(i*35)+int(metrics.text_height), body=line)
                draw(img)
                img.sample(1080, 1080)
                img.save(filename="output.png")

【问题讨论】:

标签: python python-3.x imagemagick wand


【解决方案1】:

这可能不是您要寻找的答案,但希望能让您走上正确的道路。

如何在 wand 中测量字符串的边界?

你已经这样做了。我建议使用更经典的偏移和累加器方法来映射随每次迭代更新的位置,而不是“智能快速”的单线方法。

top_margin = 120
line_offset = 0
line_padding = 35
with Drawing() as ctx:
    with Image(width=1080, height=1080, background=Color("WHITE")) as img:
        with Drawing() as draw: 
            for i,line in enumerate(lines):
                metrics = draw.get_font_metrics(img, line, multiline=True)
                draw.text(x=150, y=y=top_margin + line_offset, body=line)
                line_offset += int(metrics.text_height) + line_padding

有没有一种方法可以在边界内将长文本换行或计算文本所需的高度,以便在从数组中绘制文本时它们不会重叠。

简短的回答是。您将负责实施该算法。幸运的是,互联网上充满了可供参考的示例和研究文章。它可以像 find-the-last-space-before-overflow 一样基本......

lines = [
    'I\'m using Wand to generate a JPG with custom variable text inside it.',
    'I have an array of strings all with the same width but different heights',
    'Is there a method to word wrap a long text inside a boundary or calculate the height needed for the text so when drawing the texts from the array they don\'t overlap',
]
image_width = 540
image_height = 540
left_margin = 150
right_margin = image_width - left_margin * 2
top_margin = 120
line_padding = 35
line_offset = 0
with Drawing() as ctx:
    with Image(width=image_width, height=image_height, background=Color("LIGHTCYAN")) as img:
        with Drawing() as draw: 
            for i,line in enumerate(lines):
                metrics = draw.get_font_metrics(img, line, multiline=True)
                last_idx = 1
                # Do we need to do work?
                while metrics.text_width > right_margin:
                    last_breakpoint=0
                    # Scan text for possible breakpoints.
                    for idx in range(last_idx, len(line)):
                        if line[idx] == ' ':
                            last_breakpoint = idx
                        else:
                            # Determine if we need to insert a breakpoint.
                            metrics = draw.get_font_metrics(img, line[:idx], multiline=True)
                            if metrics.text_width >= right_margin:
                                line = line[:last_breakpoint].strip() + '\n' + line[last_breakpoint:].strip()
                                last_idx = last_breakpoint
                                break
                    # Double check any modifications to text was successful enough.
                    metrics = draw.get_font_metrics(img, line, multiline=True)
                draw.text(x=left_margin, y=top_margin + line_offset, body=line)
                line_offset += int(metrics.text_height) + line_padding
            draw(img)
            img.save(filename="output.png")

上面的代码可以优化,Python可能已经包含了一些better methods

进一步阅读...

source codeImageMagick 的CAPTION: 协议就是一个很好的例子。该算法反复调用GetMultilineTypeMetricsFormatMagickCaption 来调整点大小和插入换行符。 wand 库并不真正支持字幕协议,但您可以使用以下解决方法来使用它。

from wand.api import library
# ...
with Image(width=image_width, height=image_height, background=Color("LIGHTCYAN")) as img:
    for i,line in enumerate(lines):
        # Create a tempory image for each bounding box
        with Image() as throwaway:
            library.MagickSetSize(throwaway.wand, right_margin, line_padding)
            throwaway.read(filename='CAPTION:'+line)
            img.composite(throwaway, left_margin, top_margin + line_offset)
            line_offset += line_padding + throwaway.height
    img.save(filename="output.png")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2013-09-01
    • 2015-08-22
    • 1970-01-01
    • 2017-12-24
    相关资源
    最近更新 更多