⚠️ 这是一个非官方翻译网站,与 ImageMagick Studio LLC 无关。准确信息请参阅原文(https://usage.imagemagick.org/fonts/index.html)

ImageMagick 示例 -- 复合字体效果

ImageMagick 示例前言与索引

复合字体 以各种风格将文字绘制为图像

平铺填充渐变填充上下颠倒硬阴影切变阴影倾斜压印挤出轮廓多色轮廓描边粗描边细描边双重轮廓迷幻气球连接重叠抖动模糊模糊阴影柔和阴影柔和轮廓更浓密的柔和轮廓随距离模糊的阴影脏印刷斜面锥形内斜面拱形弧形圆形颤音彗星冒烟 在字体中使用遮罩图像 在叠加之前移除图像的某些部分 高级字体处理 扩展字体以产生更复杂的结果

更复杂的字体霓虹灯招牌金属效果 绘制文字只是 ImageMagick 所能实现功能的开始。这里我们着眼于修改 IM 的基本文字绘制能力,以创建花式字体和特殊效果,然后你就可以将其用于你的网页和文档。


复合字体效果

把普通的文字作为图像显得很乏味,但只需极少的工夫,你就可以叠加和着色文字,从而产生一些非常奇异而美妙的效果。要做到这一点,我们需要多次绘制文字、叠加不同的平铺图案和颜色,并应用许多可用的图像操作符中的一些,从原始的、乏味的文字生成更有趣的特殊效果。请注意,这些效果中有许多除了应用于像我们这里使用的基本字体之外,也可以应用于其他图像。特别是你可以将这些效果用于剪贴画图像。 平铺字体 :你不必只用固定颜色来绘制字体。你可以在字体上使用平铺图案。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -tile pattern:checkerboard   -annotate +28+68 'Anthony' \
           font_tile.jpg

[IM Output]

请注意,"[-tile](https://imagemagick.org/command-line-options/#tile)" 设置会覆盖 "[-draw](https://imagemagick.org/command-line-options/#draw)" 操作符使用的任何 "[-fill](https://imagemagick.org/command-line-options/#fill)" 颜色。 从 IM v6.3.2 起,你可以改用 "[-fill](https://imagemagick.org/command-line-options/#fill)" 设置来指定平铺图像。不过不推荐这种用法,因为许多使用 "[-fill](https://imagemagick.org/command-line-options/#fill)" 颜色的操作符无法理解平铺图像,会默认改用 'black'(黑色)。
通过在设置 "[-tile](https://imagemagick.org/command-line-options/#tile)" 图像之前指定 "[-origin](https://imagemagick.org/command-line-options/#origin)" 设置,可以相对于背景图像原点偏移平铺图像。在将该图像指定为填充平铺时,会按指定的量进行滚动。 渐变字体 :所使用的平铺图案也不必很小,而可以是整个画布的大小。
   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -tile gradient:   -annotate +28+68 'Anthony' \
           font_gradient.jpg

[IM Output]

上下颠倒字体

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill Navy     -annotate 180x180+300+35 'Anthony' \
           font_upsidedown.jpg

[IM Output]

硬阴影 :以带偏移的方式绘制字体两次,你可以制作出简单的「硬」阴影效果。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill black -draw "text 28,68 'Anthony'" \
           -fill white -draw "text 25,65 'Anthony'" \
           font_shadow.jpg

[IM Output]

切变阴影 :由于 "[-annotate](https://imagemagick.org/command-line-options/#annotate)" 字体绘制操作符可以将垂直方向与水平方向分开旋转,你可以为字体指定某种古怪的旋转「歪斜」或「切变」。这非常适合制作怪异的阴影,或者制作你自己的斜体或倾斜字体。

   magick -size 320x115 xc:lightblue  -font Candice -pointsize 72 \
           -fill Navy      -annotate 0x0+12+55   'Anthony' \
           -fill RoyalBlue -annotate 0x130+25+80 'Anthony' \
           font_slewed.jpg

[IM Output]

要查看汇总文字旋转效果的表格,请参阅 Annotate 文字选项。当然,Candice 字体并不是展示这种效果的好字体,而且可能还需要添加其他细节才能让结果看起来更像 3D。例如随距离模糊阴影 倾斜字体 :你也可以使用 "[-draw](https://imagemagick.org/command-line-options/#draw)" 来倾斜你的字体,不过这要稍微棘手一些,因为它涉及额外的 MVG(Magick Vector graphics,魔法矢量图形)动作来扭曲绘制表面。由于表面正在被扭曲,最好在扭曲之前先用 'translate' 设置字体位置。

   magick -size 320x100 xc:lightblue  -font Candice -pointsize 72 \
           -fill Navy -draw "translate 28,68  skewX -20  text 0,0 'Anthony'" \
           font_slanted.jpg

[IM Output]

要查看汇总文字旋转效果的表格,请参阅 Annotate 文字选项。当然,Candice 字体并不是展示这种效果的好字体,而且可能还需要添加其他细节才能让结果看起来更像 3D。如果你想出了什么有趣的东西,请传给我,这样就可以和世界上其他人分享了。 "[-annotate](https://imagemagick.org/command-line-options/#annotate)" 和 "[-draw](https://imagemagick.org/command-line-options/#draw) skew?" 操作实际上都是旋转绘制表面的 X 轴和 Y 轴。这与对现有图像使用 "[-shear](https://imagemagick.org/command-line-options/#shear)" 的效果不同,后者会拉长图像被切变的轴,使得图像的高度(或宽度)不会因该操作而改变。
压印字体 :通过以较深、较浅和原始颜色绘制字体三次,你可以制作出类似压印的凹痕效果。
   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill black     -annotate +24+64 'Anthony' \
           -fill white     -annotate +26+66 'Anthony' \
           -fill lightblue -annotate +25+65 'Anthony' \
           font_stamp.jpg

[IM Output]

请注意,字体的最后一次「绘制」是如何擦除字体的中间部分的。这只能在纯色背景上完成,请参阅使用遮罩图像,了解我们如何在非纯色的随机背景上使用此方法。如果你交换这两种颜色,当然就会得到凸起的字体,而不是凹陷的字体。 挤出 或 3D 块状字体:可以通过多次重复绘制字体来生成。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill gray -annotate +29+69 'Anthony' \
                      -annotate +28+68 'Anthony' \
                      -annotate +27+67 'Anthony' \
                      -annotate +26+66 'Anthony' \
                      -annotate +25+65 'Anthony' \
                      -annotate +24+64 'Anthony' \
           -fill navy -annotate +23+63 'Anthony' \
           font_extrude.jpg

[IM Output]

请注意,这不是简单的阴影,而是所绘制字体的真正加厚。这非常重复,可以用于任何「带形状」的图像。有关此方法的另一个示例,请参阅为缩略图增加厚度
轮廓字体 :我们可以通过多次绘制并施加小的位置偏移来创建轮廓字体。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill black  -annotate +24+64 'Anthony' \
                        -annotate +26+64 'Anthony' \
                        -annotate +26+66 'Anthony' \
                        -annotate +24+66 'Anthony' \
           -fill white  -annotate +25+65 'Anthony' \
           font_outlined.jpg

[IM Output]

由于这同样非常重复,它并不是一个好的轮廓解决方案。由于 ImageMagick 允许你通过设置 "[-stroke](https://imagemagick.org/command-line-options/#stroke)" 设置来绘制字体轮廓,存在更好得多的解决方案。(请参阅下面的描边字体。)尽管如此,像这样多次重绘以生成轮廓,对于预先准备好的剪贴画图像(例如你可以在互联网上随处找到的那些)可能非常有用。它对于其他不提供 "[-stroke](https://imagemagick.org/command-line-options/#stroke)" 设置的图形库和程序(如来自 "PHP" 的 "GD" 等)也是一种有用的技术。展示这种轮廓风格的另一个原因是,在为带有非常尖锐尖角的字体绘制轮廓时,结果可能更好。例如,这里我们绘制字体 12 次以凸显该字体的尖锐尖角。这里的轮廓也被画得稍微厚一些。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
            -draw "fill black text 27,67 'Anthony' \
                              text 25,68 'Anthony' \
                              text 23,67 'Anthony' \
                              text 22,65 'Anthony' \
                              text 23,63 'Anthony' \
                              text 25,62 'Anthony' \
                              text 27,63 'Anthony' \
                              text 28,65 'Anthony' \
                   fill white text 25,65 'Anthony' " \
           font_outlined_12.jpg

[IM Output]

你还会注意到,绘制用的 -fill" 颜色可以在 "[-draw](https://imagemagick.org/command-line-options/#draw)" 参数内部更改。 多色轮廓 :这种技术有用的另一个原因是,在绘制字体时你不仅限于一种轮廓颜色。通过以非常精心设计的顺序用 5 种不同颜色绘制字体 12 次,你可以制作出彩色的「凸起」字体,并带有一些边缘颜色平滑。

   magick -size 320x100 xc:lightblue \
           -font Candice -pointsize 72  -gravity center \
           -draw "fill navy         text  2,2  'Anthony' \
                  fill navy         text  0,3  'Anthony' \
                  fill navy         text  3,0  'Anthony' \
                  fill dodgerblue   text  0,2  'Anthony' \
                  fill dodgerblue   text  2,0  'Anthony' \
                  fill dodgerblue   text -2,2  'Anthony' \
                  fill dodgerblue   text  2,-2 'Anthony' \
                  fill lavender     text -2,-2 'Anthony' \
                  fill lavender     text  0,-3 'Anthony' \
                  fill lavender     text -3,0  'Anthony' \
                  fill skyblue      text  0,-2 'Anthony' \
                  fill skyblue      text -2,0  'Anthony' \
                  fill blue         text  0,0  'Anthony' " \
           font_colourful.jpg

[IM Output]

有更好的方法来创建像这样的凸起字体,但这个方法可行、简单,而且只用了少数几种颜色,而不是一整套颜色。 轮廓(描边)字体 :"[-stroke](https://imagemagick.org/command-line-options/#stroke)" 设置允许你直接绘制字体的轮廓。通常描边颜色被设为 "none"(无),因此不会被使用。描边的粗细由 "[-strokewidth](https://imagemagick.org/command-line-options/#strokewidth)" 调节,其默认值为良好的 1。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill white  -stroke black  -annotate +25+65 'Anthony' \
           font_stroke.jpg

[IM Output]

这里是一个使用更重的描边宽度 3 的示例。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill white -stroke black -strokewidth 3 \
           -annotate +25+65 'Anthony'        font_stroke_3.jpg

[IM Output]

请注意,描边颜色不仅会侵蚀字体的外侧,也会侵蚀内侧。更多细节请参阅我的描边与描边宽度选项的结果。
粗描边 :通过再次绘制字体,但这次不开启描边,线条的内部部分会被移除,从而创建出更令人愉悦的粗轮廓字体。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 -fill white \
           -stroke black -strokewidth 5 -annotate +25+65 'Anthony' \
           -stroke none                 -annotate +25+65 'Anthony' \
           font_stroke_thick.jpg

[IM Output]

使用 -stroke" 设置的方法在描边与描边宽度选项中得到了进一步深入的探讨,那里更深入地探究了绘制操作符的各个方面。 细描边 :通过关闭填充颜色,你可以只留下字体的轮廓。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill none  -stroke black   -annotate +25+65 'Anthony' \
           font_stroke_thin.jpg

[IM Output]

双重轮廓 :通过使用多种描边粗细重绘,你可以生成双重轮廓!第一次绘制可以使用任何填充颜色来填充字体内部,或者像我们这里所做的那样保持为 none 以不改动背景。但是最后一次字体绘制必须以 "none" 的填充设置进行,否则将不起作用。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
        -fill none  -stroke black  -strokewidth 3  -annotate +25+65 'Anthony' \
        -fill none  -stroke white  -strokewidth 1  -annotate +25+65 'Anthony' \
        font_stroke_double.jpg

[IM Output]

与前面的「压印字体」不同,上面的方法不需要擦除字体的中间部分。因此这将在任何背景上工作而不会出现复杂情况。 迷幻字体 :以类似的方式,通过在交换颜色的同时缓慢减小描边宽度大小,可以轻松生成迷幻轮廓效果。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 -fill white \
           -stroke black -strokewidth 25 -annotate +25+65 'Anthony' \
           -stroke white -strokewidth 20 -annotate +25+65 'Anthony' \
           -stroke black -strokewidth 15 -annotate +25+65 'Anthony' \
           -stroke white -strokewidth 10 -annotate +25+65 'Anthony' \
           -stroke black -strokewidth  5 -annotate +25+65 'Anthony' \
           -stroke none                  -annotate +25+65 'Anthony' \
           font_psychedelic.jpg

[IM Output]

你可以通过使用冲突的颜色、不同的描边宽度,甚至稍微移动字体位置,让它变得更加迷幻。多多试验,看看你能想出什么。 气球效果 :这里我做的和上面的「粗描边字体」完全一样,但纯属偶然,我在重绘字体时使用了白色描边颜色。这导致字体出现了有趣的放大,并带有粗轮廓。这种「鼓鼓的」字体就好像被像气球一样吹胀了。这正说明多试验以看看你能发现什么是值得的。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
       -fill black  -stroke black  -strokewidth 5  -annotate +25+65 'Anthony' \
       -fill white  -stroke white  -strokewidth 1  -annotate +25+65 'Anthony' \
       font_balloon.jpg

[IM Output]

连接字符 :通过使用一个小的负字符间距字距调整设置(IM v6.4.7-10 新增)并绘制字体两次(如前一个示例所示),你可以让「粗」字体中的所有字符连接在一起,从而产生有趣的变体。

  magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
          -kerning -6  -strokewidth 4 -fill white \
          -stroke black   -annotate +28+68 Anthony \
          -stroke none    -annotate +28+68 Anthony \
       font_joined.jpg

[IM Output]

重叠字符 :然而对此的一种变体是分别绘制每一个字符,使每个字符都叠加在前面字符的上方。

  magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
          -stroke black -strokewidth 4 -fill white \
          -stroke black -annotate  +28+68 A  -stroke none -annotate  +28+68 A \
          -stroke black -annotate  +90+68 n  -stroke none -annotate  +90+68 n \
          -stroke black -annotate +120+68 t  -stroke none -annotate +120+68 t \
          -stroke black -annotate +138+68 h  -stroke none -annotate +138+68 h \
          -stroke black -annotate +168+68 o  -stroke none -annotate +168+68 o \
          -stroke black -annotate +193+68 n  -stroke none -annotate +193+68 n \
          -stroke black -annotate +223+68 y  -stroke none -annotate +223+68 y \
          font_overlapped.jpg

[IM Output]

然而这需要你(手动或使用脚本自动地)算出每个字符的合适位置。每个字符的自然宽度可以通过为每个字符生成不带任何 "[-strokewidth](https://imagemagick.org/command-line-options/#strokewitdh)" 设置的标签来确定。示例请参阅确定字体度量。请注意,与使用 "[-kerning](https://imagemagick.org/command-line-options/#kerning)" 设置(前一个示例)时不同,上面每个字符的位置是按不同的量进行艺术性调整的,而不仅仅是某个简单的固定量。例如,'t' 和 'h' 之间只有一点点重叠,但 'n' 和 'y' 字符之间的重叠则多得多。 抖动字符 :如果你已经做到了绘制单个字符(无论是否重叠),那么你就可以以「抖动」或随机化的图案来放置它们,尤其是带有不同的上下偏移。你甚至可以将其推向极致以生成某种特殊效果,例如……

  magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
          -stroke black -strokewidth 4 -fill white \
          -stroke black -annotate  +26+80 A  -stroke none -annotate  +26+80 A \
          -stroke black -annotate  +95+63 n  -stroke none -annotate  +95+63 n \
          -stroke black -annotate +133+54 t  -stroke none -annotate +133+54 t \
          -stroke black -annotate +156+67 h  -stroke none -annotate +156+67 h \
          -stroke black -annotate +193+59 o  -stroke none -annotate +193+59 o \
          -stroke black -annotate +225+59 n  -stroke none -annotate +225+59 n \
          -stroke black -annotate +266+54 y  -stroke none -annotate +266+54 y \
          font_jittered.jpg

[IM Output]

模糊字体 :使用 "[-blur](https://imagemagick.org/command-line-options/#blur)" 操作符直接扩散字体颜色。这个操作符允许你取一幅图像并使其向所有方向扩散。这使你能够生成看起来更柔和的阴影,或类似喷漆的效果。以下示例展示了你可以用它实现的效果。

  magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
          -fill navy  -annotate +25+65 'Anthony' \
          -blur 0x3   font_fuzzy.jpg

[IM Output]

请注意,模糊是在给出该操作符的那一刻应用于整幅图像的。如果你想对现有图像使用模糊,你将不得不分别绘制字体(在透明背景上),然后将其叠加到背景图像上。 "[-blur](https://imagemagick.org/command-line-options/#blur)"(或 "[-gaussian](https://imagemagick.org/command-line-options/#gaussian)")操作符修改的区域比你可能预想的要大得多。如果你的背景画布不够大,你可能会从这些操作符得到错误。如果发生这种情况,请为图像添加额外空间,比如使用 "[-border](https://imagemagick.org/command-line-options/#border)",或为该操作符的工作半径(第一个参数)添加一个限制。 此外,对图像进行模糊通常会使得之后使用 "[-trim](https://imagemagick.org/command-line-options/#trim)" 变得相当无用。每当你对图像使用模糊时,可能都需要手动裁剪或进行其他调整。
模糊阴影 :使用模糊字体作为偏移以制作柔和阴影。请注意,我们还使用了较大的扩散值。
  magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
          -annotate +30+70 'Anthony'   -blur 0x4 \
          -fill white  -stroke black  -annotate +25+65 'Anthony' \
          font_shadow_fuzzy.jpg

[IM Output]

柔和阴影 :"[-shadow](https://imagemagick.org/command-line-options/#shadow)" 操作符不仅允许你为包含透明度的图像生成并定位柔和的模糊阴影,还允许你设置使用任何颜色并设置整体透明度级别。

  magick -size 300x100 xc:none -font Candice -pointsize 72 \
          -fill white  -stroke black  -annotate +25+65 'Anthony' \
          \( +clone -background navy  -shadow 70x4+5+5 \) +swap \
          -background lightblue -flatten  -trim +repage  font_shadow_soft.jpg

[IM Output]

有关 "[-shadow](https://imagemagick.org/command-line-options/#shadow)" 操作符的更多信息,请参阅生成阴影。从 IM v6.3.1 起,"[montage](montage.html)" 命令也可以为包含透明度的图像生成柔和的「带形状」阴影。这意味着你可以非常容易地为 "[label:](text.html#label)" 图像添加阴影。

  montage -background none -fill white -font Candice \
          -pointsize 72 label:'Anthony' +set label \
          -shadow  -background lightblue -geometry +5+5 \
          font_montage_shadow.jpg

[IM Output]

然而,你对 montage 阴影的偏移、颜色或模糊量没有任何控制权(目前如此)。 柔和轮廓 :使用模糊字体作为轮廓边框。这就像把原始字体当作喷枪的遮罩来使用。

  magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
          -annotate +25+65 'Anthony'    -blur 0x5 \
          -fill white  -annotate +25+65 'Anthony'   font_outline_soft.jpg

[IM Output]

请注意,边缘非常浅,因为不仅黑色向外扩散,背景颜色也向内扩散,使得边缘只有 50% 的深度。解决此问题的一种方法是使用阴影轮廓,并配合色阶调整来修正那种浅淡,不过这使用了一些非常高级的图像处理技术。 更浓密的柔和轮廓 :修正柔和轮廓浅淡问题的另一种方法是对带有较宽描边轮廓的字体进行模糊。这有效地将 50% 的模糊点移到了离字体边缘更远的位置。它还允许使用更大的模糊值,从而让黑色扩散得更远。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -stroke black -strokewidth 8 -annotate +25+65 'Anthony' -blur 0x8 \
           -fill white   -stroke none   -annotate +25+65 'Anthony' \
           font_denser_soft_outline.jpg

[IM Output]

作为此方法的实际示例,请参阅在为缩略图添加图像标签以及在图像顶部标注中最后一个示例所生成的示例。
随距离模糊的阴影 :随着可变模糊映射的引入,你现在可以对阴影进行模糊处理,使其随着该阴影与投射对象的视距增加而变得更加模糊。例如,这里我取了切变阴影字体并对阴影进行模糊处理,使其在顶部不模糊,而在底部更模糊。

   magick -size 320x40 xc:lightblue  -font Candice -pointsize 72 \
           -fill RoyalBlue -annotate 0x125+20+0 'Anthony' \
           \( -size 320x45 gradient:black -append \) \
           -compose Blur -set option:compose:args 20x5+45 -composite \
           \( -size 320x60 xc:lightblue \
              -fill Navy    -annotate 0x0+20+59   'Anthony' \) \
           +swap -append   font_var_blur.jpg

[IM Output]

请注意,我没有只使用圆形模糊,因为照射在倾斜表面上的光会形成椭圆,而不是圆。因此模糊也需要形成椭圆。基本上我使用了椭圆模糊的变体来实现这种效果。最后一点,使用 annotate 角度参数来创建切变文字(参见 Annotate 参数用法),可能不是像这样生成初始 3D 阴影的最佳方式。基本上它无法像真实阴影那样使阴影变短或变长,因为它只做旋转切变。更好的方法是使用三点仿射扭曲,它能让你更好地控制阴影的放置(参见 3D 阴影,使用仿射切变)。当然,你仍然需要可变模糊技术来使它看起来正确。
脏印刷字体像素被稍微扩散开,然后被模糊,并被阈值化几次以平滑最终的轮廓。结果是一种看起来像印在粗糙报纸上的字体。

  magick -size 320x100 xc: \
          -font Candice -pointsize 72 -annotate +25+65 'Anthony' \
          -spread 1 -blur 0x1 -threshold 50% -blur 0x1 font_dirty_print.jpg

[IM Output]

这种字体来自讨论如何把字体弄脏,其中包含其他更复杂的方法。
斜面字体Shade 操作符可用于生成带有斜面和平滑弯曲边缘的非常漂亮的 3D 字体。

    magick -size 320x100 xc:black -font Candice -pointsize 72 \
              -fill white   -annotate +25+65 'Anthony' \
              -shade 140x45  font_beveled.jpg

[IM Output]

这比压印字体好得多,但 shade 只会生成灰度图像。另一方面,有许多方法可以用你所需的任何颜色替换上述结果的灰度。使用 shade 来给字体「加斜面」的最大问题是,斜面的厚度不易调整。它基本上固定在大约 5 像素的厚度,无论所用字体大小如何。
锥形字体 :通过使用新的形态学距离方法(从 IM v6.6.2 起)结合 Shade 操作符,你可以让整个字体看起来像一座三维的山脊。这确实需要按照对抗锯齿形状的距离对抗锯齿像素进行一些特殊处理,但结果是一种圆锥形的、山一样的字体。

  magick -size 320x100 xc:black -font Candice -pointsize 72 \
          -fill white   -annotate +25+65 'Anthony' \
          -gamma 2  +level 0,1000 -white-threshold 999 \
          -morphology Distance Euclidean:4,1000 -auto-level \
          -shade 135x30 -auto-level +level 10,90% font_conic.jpg

[IM Output]

通过添加一点 "[-adaptive-blur](https://imagemagick.org/command-line-options/#adaptive-blur)",你可以平滑结果,为最终字体生成更好且奇特闪亮的外观。

  magick -size 320x100 xc:black -font Candice -pointsize 72 \
          -fill white   -annotate +25+65 'Anthony' \
          -gamma 2  +level 0,1000 -white-threshold 999 \
          -morphology Distance Euclidean:4,1000 -auto-level \
          -shade 135x30 -auto-level +level 10,90% \
          -adaptive-blur 0x2  font_conic_smoothed.jpg

[IM Output]

把 "[-adaptive-blur](https://imagemagick.org/command-line-options/#adaptive-blur)" 移到使用 Shade 之前,会使边缘被模糊,但不会模糊字体形状的中央山脊(骨架)。结果看起来就像锐利的山脊正向上顶入一张橡胶板。

  magick -size 320x100 xc:black -font Candice -pointsize 72 \
          -fill white   -annotate +25+65 'Anthony' \
          -gamma 2  +level 0,1000 -white-threshold 999 \
          -morphology Distance Euclidean:4,1000 -auto-level \
          -adaptive-blur 0x2 \
          -shade 135x30 -auto-level +level 10,90%  font_conic_ridge.jpg

[IM Output]

使用不同的距离核,例如 Chebyshev,对更规整的字体(例如 Arial 字体系列)效果更好。

  magick -size 320x100 xc:black -font ArialBk -pointsize 70 \
          -fill white   -annotate +5+70 'Anthony' \
          -gamma 2  +level 0,1000 -white-threshold 999 \
          -morphology Distance Chebyshev:1,1000 -auto-level \
          -shade 135x30 -auto-level +level 10,90% font_chebyshev.jpg

[IM Output]

内斜面字体 :通过裁剪距离函数来限制距离,将只对形状的边缘加斜面。

  magick -size 320x100 xc:black -font Candice -pointsize 72 \
          -fill white   -annotate +25+65 'Anthony' \
          -gamma 2 +level 0,1000 -white-threshold 999 \
          -morphology Distance Euclidean:4,1000  -level 0,5000 \
          -shade 135x30 -auto-level +level 10,90% font_inner_bevel.jpg

[IM Output]

拱形字体 :"[-wave](https://imagemagick.org/command-line-options/#wave)" 操作符(详情参见正弦波位移)会垂直移动图像的像素,以形成拱形。竖线将保持竖直,字符会被切变以产生曲线。

    magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
    -fill navy  -annotate +25+65 'Anthony' \
    -background lightblue -wave -50x640 -crop x110+0+10 \
    font_wavy.jpg

[IM Output]

请注意,要用 "[-wave](https://imagemagick.org/command-line-options/#wave)" 创建拱形,你需要使用为图像宽度两倍的「波长」(2 × 320 即 640 像素)。此外,由于 "[-wave](https://imagemagick.org/command-line-options/#wave)" 会按其拱起的量为图像添加额外空间,因此之后需要把那个空间修剪或裁剪掉。这是一种简单、快速但有效的制作文字拱形的方法。 弧形字体通用扭曲操作符还提供其他文字/图像扭曲方法。例如 '[Arc](distorts.html#arc)'(弧)方法会将字体弯曲成真正的圆弧,而不是前一示例中那种垂直移位的「拱形」。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill navy -annotate +25+65 'Anthony' \
           -distort Arc 120  -trim +repage \
           -bordercolor lightblue -border 10  font_arc.jpg

[IM Output]

圆形字体 :你甚至可以把它推向极端,将文字环绕成一个完整或几乎完整的圆。

   magick -font Candice -pointsize 32 -background lightblue \
           -fill navy label:"Anthony's IM Examples" \
           -virtual-pixel background  -distort Arc 340 \
           font_circle.jpg

[IM Output]

更多选项和可能性请参阅弧扭曲 螺旋字体 :在环绕之前添加一点旋转以使字体倾斜一个角度,圆就可以被转换成螺旋。

   magick -font Candice -pointsize 32 -background lightblue \
           -fill navy  label:"Anthony's IM Examples" \
           -rotate 12 -virtual-pixel background -distort Arc 360 \
           -trim -bordercolor lightblue -border 5x5  font_spiral.jpg

[IM Output]

然而文字的高度(径向上的)保持不变,它不会随着越接近中心而被拉伸或压缩,从而对字符产生强烈的纵横比扭曲。你可以通过在文字旋转过程中使用透视扭曲来解决这个问题,在应用弧扭曲之前调整字体高度。这种技术的问题在于你只能做螺旋的一圈,不过通过多行文字并小心地对齐各行,你可以生成多重螺旋。如果你尝试了这个,请把示例提交回给我?
颤音字体 :我们在上面的拱形字体中使用的 "[-wave](https://imagemagick.org/command-line-options/#wave)" 操作符,也可以以更高的频率和更小的振幅来使用,以制作振动的字体。此外,通过添加一些旋转操作,你甚至可以让振动以你喜欢的任何角度进行!

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill navy  -annotate +25+65 'Anthony' \
           -background lightblue -rotate 85  -wave 2x5   -rotate -85 \
           -gravity center  -crop 320x100+0+0 +repage font_vibrato.jpg

[IM Output]

有关像这样使用扭曲的更多信息,请参阅图像变形示例页面,尤其是波扭曲操作符 彗星字体 :专门的模糊操作符之一 "[-motion-blur](https://imagemagick.org/command-line-options/#motion-blur)" 允许你为图像中的对象创建类似彗星的尾巴。

   magick -size 340x120 xc:lightblue  -font Candice  -pointsize 72 \
           -fill navy   -annotate +45+95 'Anthony' -motion-blur 0x25+65 \
           -fill black  -annotate +45+95 'Anthony' -motion-blur 0x1+65 \
           font_comet.jpg

[IM Output]

你可以通过使用不同的颜色让这种复合字体生动起来,制作出真正火热的景象。你还可以用专门的模糊做更多事情,不过 IM 的这整个方面仍是实验性的,这些操作符的语法在不久的将来可能会改变。 冒烟字体 :将这个与 wave 结合,你就可以让彗星字体看起来像有烟、气味,甚至火焰正从字体上升起!

   magick -size 320x120 xc:lightblue  -font Candice  -pointsize 72 \
           -fill black  -annotate +25+95 'Anthony'  -motion-blur 0x25+90 \
           -background lightblue -rotate 60  -wave 3x35  -rotate -60 \
           -gravity center  -crop 320x120+0+0 +repage +gravity \
           -fill navy   -annotate +25+95 'Anthony'   font_smoking.jpg

[IM Output]

你有有趣的变换想添加到上面的列表中吗?


在字体中使用遮罩图像

在背景图像上绘制「压印字体」实际上比我上面详述的大多数生成花式字体的方法困难得多。原因是原始字体的一部分被擦除了,当在非简单纯色的背景上绘制它时,这就带来了一个问题。这是我们上面生成的复合字体图像。

   magick -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -fill black      -annotate +24+64 'Anthony' \
           -fill white      -annotate +26+66 'Anthony' \
           -fill lightblue  -annotate +25+65 'Anthony' \
           font_stamp.jpg

[IM Output]

如果我们尝试用完全相同的方法在透明背景(PNG 格式图像)上绘制字体,我们只会失败……

   magick -size 320x100 xc:transparent -font Candice -pointsize 72 \
           -fill black        -annotate +24+64 'Anthony' \
           -fill white        -annotate +26+66 'Anthony' \
           -fill transparent  -annotate +25+65 'Anthony' \
           trans_stamp.png

[IM Output]

所发生的是,我们试图用「透明」颜色擦除字体的中心部分。但正如你所见,用透明度绘制只是什么都不画!所以上面的结果就和最后那个「擦除」-annotate" 操作符完全不起作用一样。这个问题大约有六七种解决方案。我将在这里介绍其中三种方法,而其他方法在 IM 示例页面的各种其他地方有讨论。可能最简单的方法是根本不去修复上面的字体,而是在把它绘制到背景图像上时使用一个「遮罩」,来告诉 "magick composite" 命令忽略中间部分。"magick composite" 遮罩图像是一幅灰度图像,对于将变为透明的部分用纯黑,对于任何你想要完全可见(不透明)的部分用纯白。任何灰色调都会被绘制为半透明,与下面的背景颜色融合。嗯,我们上面的图像已经几乎正确了,所以让我们把所有不想要的部分遮罩掉。我们从黑色背景(完全透明)开始,然后用白色绘制任何我们想要的东西,用黑色绘制任何我们不想要的东西。

   magick -size 320x100 xc:black -font Candice -pointsize 72 \
           -fill white   -annotate +24+64 'Anthony' \
           -fill white   -annotate +26+66 'Anthony' \
           -fill black   -annotate +25+65 'Anthony' \
           mask_mask.jpg

[IM Output]

请注意,我们创建的遮罩 只是黑白的,而是由于对抗锯齿(详情参见对抗锯齿示例)在各区域边缘有各种灰色调。这些灰色像素使最终图像的边缘更平滑,而且非常重要,因为这些像素代表半透明(只有一半可见)的像素。现在我们有了遮罩,就可以遮罩掉图像中不想要的部分。我们可以在把图像绘制(叠加)到等离子背景上时这样做。请注意所有三个图像参数的顺序(字体、背景,然后遮罩)。

  magick -size 320x180 plasma: -shave 0x40 plasma_background.jpg
  magick composite trans_stamp.png   plasma_background.jpg   mask_mask.jpg \
            mask_result.jpg

[IM Output]

三参数的 "magick -composite" 形式是……

  magick plasma_background.jpg  trans_stamp.png  mask_mask.jpg \
          -composite  mask_result2.jpg

[IM Output]

结果不错,但我们现在需要两幅图像来绘制复合字体。如果只需要一幅图像,并将遮罩直接构建进图像本身,那就更好了。基本上我们想用我们创建的字体图像遮罩的副本来完全替换字体图像的 alpha 通道。也就是说,我们将字体图像(提供像素的颜色)直接与其遮罩(提供图像的 alpha 通道)合并。alpha 合成设置 '[CopyOpacity](compose.html#copyopacity)' 执行这种替换。请注意命令中参数的顺序。在这种情况下,字体本身是背景图像,而遮罩是被叠加进背景图像的图像。

  magick composite -compose CopyOpacity   mask_mask.jpg   trans_stamp.png \
            trans_stamp3.png
  magick composite trans_stamp3.png   plasma_background.jpg  mask_result3.jpg

[IM Output] [IM Output]

这一切的结果是,字体的中心部分终于通过被设为完全透明而被正确地擦除了。因此,所得的单一图像可以轻松地叠加到任何背景上,而不需要任何额外的遮罩图像。 把像上面这样的较大透明字体压印缩小,会制作出非常好的水印。缩小会使轮廓不那么不透明、不那么显眼,正如水印应有的那样。这两种颜色还确保该标记在非常浅和非常深的图像上都能可靠地可见。
有关使用图像遮罩的更多信息,请参阅编辑图像遮罩

高级字体处理

通过将上面的技术组合在一起,配合适当的颜色和其他可用的花式字体,你可以制作出一些奇妙的效果,它们看起来常常与上面展示的原始技术完全不同。

更复杂的字体——一个示例

例如,这里我们生成一段非常复杂且色彩缤纷的文字。

  magick -font Times-Bold -pointsize 64 \
                     -background none  label:"Colorful Arc" \
          \( +clone -sparse-color Barycentric '0,%h blue %w,0 red' \
             \) -compose In -composite \
          -virtual-pixel transparent -distort arc 120 \
          \( +clone -background black -shadow 100x2+4+4 \
             \) +swap -background white -compose over -layers merge +repage \
          colorful_arc.jpg

[IM Output]

上面这个复杂命令的布局方式,是让每个主要的图像处理步骤都在单独的行上进行。首先创建文字图像。然后生成一个两点渐变作为颜色叠加。也可以使用其他着色方法,例如模糊随机图像分形等离子平铺画布。然后使用该颜色叠加,借助 In Alpha 合成操作符为文字着色。接着使用弧扭曲对着色后的文字进行扭曲,最后生成一个阴影图层合并到文字下方。最后那个方法还会移除透明度,在保存为 JPEG 之前用白色替换它。有关此类复杂图像处理的更多信息,请参阅复杂图像处理与调试

霓虹灯招牌

这是另一个简单的示例。通过在深色背景上使用柔和轮廓字体,但全部使用相同的颜色,并配合适当的字体,你可以生成简单的「霓虹灯招牌」效果……

  magick -fill dodgerblue -background black -font Anaconda -pointsize 72 \
          label:' I M  Examples ' -bordercolor black -border 30x30 \
          \( +clone -blur 0x25 -level 0%,50% \) \
          -compose screen -composite    neon_sign.gif

[IM Output]

只需再多做一点工作,你甚至可以让它动起来!但我会把那个留作读者的练习。有关诸如「凝胶」效果「水」效果之类的特殊效果,请参阅高级 IM 示例。对于你可能想尝试实现的其他字体效果的一些示例,请参阅 XaraXone,使用轮廓工具。IM 可以轻松创建诸如其中所示的轮廓效果。

金属效果

这种效果本质上是一种圆化以及颜色查找表替换效果。请参阅讨论金属效果。可用示例由 snibgo 提供

# 生成一个模糊的输入字体形状遮罩
# 第一个模糊-色阶是一种圆化或「积水」效果
# 第二个模糊是金属效果的关键所在。
magick -background none -pointsize 160 -font Candice label:" Anthony " \
        -blur 0x5 -channel A -level 40%,60% +channel \
        -blur 0x3    metallic_input.png

# 金属颜色查找表
magick \
  -size 1x1000 gradient:  -gamma 0.9 \
  -function Sinusoid 2.25,0,0.5,0.5 \
  \( gradient:'rgb(100%,100%,80%)-black' -gamma 1 \) \
  +swap \
  -compose Overlay -composite \
  -rotate 90 \
  metallic_clut.png

# 给模糊的字体一个金属外观
#  * 首先创建一个垂直渐变
#  * 然后将其与一个 'shade' 反射渐变合并
#  * 再将颜色应用于所得的渐变
#  * 最后添加一个阴影。
magick metallic_input.png -set colorspace RGB \
  \( -clone 0 -alpha off \
     -sparse-color Barycentric "0,0 White  0,%[fx:h-1] Black" \
     -alpha on \
  \) \
  \( -clone 0 -alpha extract -shade 135x45 -auto-gamma -auto-level \
     -alpha on -channel A -level 0%x10% +channel \
  \) \
  -delete 0 -compose Overlay -composite \
  metallic_clut.png -clut  -set colorspace sRGB \
  \
  \( +clone -background navy -shadow 80x2+5+5 \
  \) +swap -background None -compose Over -layers merge \
  \
  -trim +repage metallic.png

[IM Output]