【问题标题】:ImageMagick: Why won't this text expand to fill?ImageMagick:为什么这个文本不能扩展填充?
【发布时间】:2018-02-05 00:01:50
【问题描述】:

我正在阅读labeltext handling docs,似乎如果我指定-size,它应该会神奇地变得尽可能大以填充空间。

我想让这个文本尽可能大。我会自己在标题中设置\n 字符(如果绝对必要的话)。

我宁愿 \n 自动计算,但似乎 label 是最大可能的(但不支持自动 \n),而 caption 将添加 \n 其中合适(但不支持填充以适应空间的动态大小)。

我的目标是基本上获得最大可能的文本,无论是在 1 行还是 3 行,以适合绿色框(在 2 红色 ****** 行之间)。

以下是它不会改变的 2 个示例。绿色只是暂时的,所以我可以调试。

这是我的这段文字的代码(没有其余部分):

-size 290x54 -background green -fill blue -font ArialB -gravity center label:'Join Us'

多行...

-size 290x54 -background green -fill blue -font ArialB -gravity center label:'Join Us Tomorrow\nFor An HVAC Meeting'

我不知道这是否可能,但理想情况下它应该问“如果标题在 1 行,它可能的最大大小是多少?如果我在第 1 个单词后面加上 \n,那么可能的最大尺寸?如果我在第 2 个单词后面加上 \n,它可能的最大尺寸是多少?”...然后选择其中最大的一个。

【问题讨论】:

  • 标签:如果你放入\n,将不会扩展到全宽。如果你想让它全宽和多行,请不要使用 \n 并使用标题:仅指定宽度(无高度)和所需的点大小。
  • 但是,如果我想让 pointsize 变成最大的文本以适应盒子怎么办? (就像在文本文档中一样,安东尼如何填充以适应空间)。有时标题只有 3 个字,我希望它很大,有时它会是 5 或 10 个字,我希望它尽可能大,但不要太大
  • 我添加了 2 张可能会更好的图片

标签: image imagemagick imagemagick-convert


【解决方案1】:

您的命令对我有用,只需进行一次更正。显然,pointsize 从您的主命令泄漏到括号处理中,即使我添加了 -respect-parenthesis。所以解决方案是在括号命令中添加+pointsize。这是我的命令和结果。

magick -respect-parenthesis -size 298x248 canvas:white \( 'arrows.png' -resize 300x \) -gravity northwest -geometry +0+140 -compose over -composite -bordercolor '#cccccc' -border 1 -font Arial -fill red -pointsize 22 -gravity north -annotate +0+4 '*************************' -font Arial -fill red -pointsize 22 -gravity north -annotate +0+68 '*************************' -font Arial -fill green -pointsize 15 -gravity south -annotate +0+64 'hvac.com' \( -size 279x55 +pointsize -background green -fill blue -font ArialB -gravity center caption:'test headline' \) -gravity center -geometry +0-82 -compose over -composite  output.png


请注意,我将 arrows.png 文件放在可以访问它的位置,与发出命令的位置相同。您可以更改它的路径。

【讨论】:

  • 你是男人中的神...我会在大约10分钟内尝试giphy.com/gifs/iiS84hOJXh1Pq
  • 有效!!!上帝,你太棒了……谢谢你。世界各地的暖通空调管道站点将感谢您
【解决方案2】:

在 ImageMagick 中,通过指定 widthxheight 而不是 pointsize 来使用标题。 ImageMagick 会找出合适的磅值来将文本填充到框中。例如:

convert -size 500x200 -background white -font arial -fill black -gravity center caption:"Testing" -bordercolor red -border 1 test1.png

convert -size 500x200 -background white -font arial -fill black -gravity center caption:"Should Be Bigger" -bordercolor red -border 1 test2.png

convert -size 500x200 -background white -font arial -fill black -gravity center caption:"This is a title that will be fairly long and I want it to fit nicely" -bordercolor red -border 1 test3.png

您也可以包含磅值,只要您的文本对于框的区域来说不会太长。

convert -size 500x200 -background white -font arial -fill black -pointsize 48 -gravity center caption:"Testing" -bordercolor red -border 1 test1b.png

convert -size 500x200 -background white -font arial -fill black -pointsize 48 -gravity center caption:"Should Be Bigger" -bordercolor red -border 1 test2b.png

convert -size 500x200 -background white -font arial -fill black -pointsize 48 -gravity center caption:"This is a title that will be fairly long and I want it to fit nicely" -bordercolor red -border 1 test3b.png

但是,如果你选择的字体太大,你会得到这个:

convert -size 500x200 -background white -font arial -fill black -pointsize 64 -gravity center caption:"This is a title that will be fairly long and I want it to fit nicely" -bordercolor red -border 1 test3c.png

【讨论】:

  • 嗯..这就是我尝试过的,但我得到了这个:imgur.com/a/9hO2w——这是我正在使用的代码。它在 ruby​​ 中,而不是在 imagemagick 命令行中,但 stack.do |stack| ... end\( ... \) 相同:imgur.com/a/79Hjw
  • 我上面的命令应该对 IM 7 使用“magick”而不是“convert”。我使用 IM 7.0.7.22 和 magick 得到的结果与上面对 IM 6 和转换的结果相同。也许您的 Rmagick 需要 IM 6 而不是 IM 7,或者您在使用 IM 7 时需要在命令中使用 magick 而不是转换。
  • 我不知道 ruby​​ 或 Rmagick。但是使用 ImageMagick 7,您可以安装旧版转换组件,但这将使用旧版 IM 6 代码。问题在于 Ruby/Rmagick 是期待 IM 6 还是 IM 7。即使它与 IM 7 兼容,它也可能期待 IM 7 magick 命令而不是 IM 6 转换。如果它可以在传统模式下使用带有转换的 IM 7,那么您必须安装 IM 7 以便它可以使用旧代码。对于 Windows,这是安装时的一个特殊标志。对于 Mac 和 Unix,使用了符号链接并且可能已经存在。您需要与 Rmagick 开发人员核实
  • arrows.png 文件在哪里?它不会显示在您的结果中。是否可以通过您提供的路径访问?请提供该图像,以便可以测试您的命令。我也没有在您的输出中看到“hvac.com”的文本。您确定您实际上正在运行该命令吗?您的输出是来自命令行还是来自 Rmagick?
  • 很好奇有人否决了我的回答!我想知道这是什么原因?我的回答有什么问题?太糟糕了,人们没有给出拒绝投票的理由。知道原因会很有帮助,这样就不会重蹈覆辙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2021-05-06
  • 2020-02-04
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
相关资源
最近更新 更多