ImageMagick(或其他),用于将UTF-8文本转换为图像


8

我正在尝试使用创建许多单字符图像convert。目的是为塑料专家提供可以打印到键盘键帽上的图像。

ImageMagick的文件显示是这样的:

 printf "\u2318" | convert -size 100x100 label:@- -font unifont-Medium command.png

要么

 echo ⌘ | convert -size 100x100 label:@- -font unifont-Medium command.png

要么

 convert -size 100x100 label:'⌘' -font unifont-Medium command.png

应该打印兴趣点符号,但会产生一个问号。

我已经确认(使用charmapunifont-Medium字体集实际上包括此字形。我知道我的系统上的其他字体也可以提供此字形,因为在安装unifont之前我可以看到它,但是我无法弄清它们的名字。

我也尝试过使用LaTeX(例如,带有所有其他功能和utf8x软件包的TexLive ),但不支持任何特殊字符。

论坛上有很多有关使用imagemagick和其他特殊字符打印汉字的问题...以下对我有用:

convert -background lightblue -fill blue -pointsize 32 \
      label:' é è à ù ç Ö ÿ ‘ ’ “ ” ° ² ³ € x ÷ '    label_i8n.gif

但对于实际上对我来说很重要的UTF-8字符却不起作用。

如何将任意unicode字符转换为图像?

更新:我放弃了,看起来像个错误。相反,我创建了一堆脚本来生成和渲染SVG,在此处开源:kinesis-dvorak


我已经很长时间没有使用它了,但是我很肯定您可以使用一个简单的gimp的 “ script-fu”例程来做到这一点。除非您熟悉gimp并且知道方案或移植该插件的另一种语言(python,perl,tcl,可能还有更多),否则这对您可能没有太大作用-但是我虽然抛出了以防万一。
goldilocks 2014年

以上对我也不起作用。好像是imagemagick中的错误,不是吗?
slm

顺便说一句。我提出了一个相关的问题:如何将多种字体组合为一种
Grzegorz Wierzowiecki

Answers:


10

我尝试了相同的命令并获得了相同的结果。

$ printf "\u2318" | convert -size 100x100 label:@- \
        -font unifont-Medium command.png

                                                                           渲染字体的ss

但是,将字母G更改为unicode可以正常工作:

$ printf "\u0047" | convert -size 100x100 label:@- \
       -font unifont-Medium command.png

                                                                           G

我会将这个问题发布到ImageMagick Discourse网站上,以查看为什么会发生这种情况。如果您不确定如何执行或继续操作,我们可以为您提供帮助。

调试转换

您可以添加-debug annotate开关以查看最新情况convert

$ printf "\u2318" | convert -size 100x100 label:@- -font unifont-Medium -pointsize 40 -debug annotate command.png
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 12
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 6.375; height: 14; ascent: 11; descent: -3; max advance: 24; bounds: 0.625,0  6.78125,8; origin: 7,0; pixels per em: 12,12; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 12
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 6.375; height: 14; ascent: 11; descent: -3; max advance: 24; bounds: 0.625,0  6.78125,8; origin: 7,0; pixels per em: 12,12; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 24
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 12.75; height: 28; ascent: 22; descent: -5; max advance: 48; bounds: 1.25,0  13.5781,18; origin: 15,0; pixels per em: 24,24; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 48
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 25.5156; height: 55; ascent: 43; descent: -10; max advance: 96; bounds: 2.48438,0  27.1406,35.5; origin: 29,0; pixels per em: 48,48; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.010u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 96
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 53.0312; height: 110; ascent: 87; descent: -20; max advance: 192; bounds: 4.96875,0  54.2812,70; origin: 59,0; pixels per em: 96,96; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 96
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 53.0312; height: 110; ascent: 87; descent: -20; max advance: 192; bounds: 4.96875,0  54.2812,70; origin: 59,0; pixels per em: 96,96; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 95
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 52.0781; height: 109; ascent: 86; descent: -20; max advance: 190; bounds: 4.92188,0  53.7188,70; origin: 58,0; pixels per em: 95,95; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 94
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 51.1406; height: 108; ascent: 85; descent: -20; max advance: 188; bounds: 4.85938,0  53.1562,68; origin: 57,0; pixels per em: 94,94; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 93
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 51.1875; height: 107; ascent: 84; descent: -20; max advance: 186; bounds: 4.8125,0  52.5781,67; origin: 57,0; pixels per em: 93,93; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 92
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 50.2344; height: 106; ascent: 83; descent: -19; max advance: 184; bounds: 4.76562,0  52.0156,67; origin: 56,0; pixels per em: 92,92; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.020 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 91
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 50.2969; height: 105; ascent: 82; descent: -19; max advance: 182; bounds: 4.70312,0  51.4531,65.4688; origin: 56,0; pixels per em: 91,91; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 90
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 49.3438; height: 103; ascent: 81; descent: -19; max advance: 180; bounds: 4.65625,0  50.8906,65.4688; origin: 55,0; pixels per em: 90,90; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 89
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 48.3906; height: 102; ascent: 81; descent: -19; max advance: 178; bounds: 4.60938,0  50.3281,64; origin: 54,0; pixels per em: 89,89; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 88
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 48.4375; height: 101; ascent: 80; descent: -19; max advance: 176; bounds: 4.5625,0  49.7656,64; origin: 54,0; pixels per em: 88,88; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 87
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 47.5; height: 100; ascent: 79; descent: -18; max advance: 174; bounds: 4.5,0  49.1875,63; origin: 53,0; pixels per em: 87,87; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 86
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 47.5469; height: 99; ascent: 78; descent: -18; max advance: 172; bounds: 4.45312,0  48.625,63; origin: 53,0; pixels per em: 86,86; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 86
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/GetTypeMetrics/798/Annotate
  Metrics: text: ⌘; width: 47.5469; height: 99; ascent: 78; descent: -18; max advance: 172; bounds: 4.45312,0  48.625,63; origin: 53,0; pixels per em: 86,86; underline position: -5.0625; underline thickness: 3.35938
2014-04-10T09:37:04-04:00 0:00.030 0.020u 6.7.8 Annotate convert[16619]: annotate.c/RenderFreetype/1220/Annotate
  Font /usr/share/fonts/msttcorefonts/arialbd.ttf; font-encoding none; text-encoding none; pointsize 86

更新#1-进一步调试

这个问题让我很烦,所以我想我终于明白了。问题在于字体的选择,并且无法显示该特定字形。

  1. 首先,您可以使用此命令查看其中可用的字体convert。因此,让我们从那里开始。

    $ convert -list font | head -8
    
    Path: /etc/ImageMagick/type-ghostscript.xml
      Font: AvantGarde-Book
        family: AvantGarde
        style: Normal
        stretch: Normal
        weight: 400
        glyphs: /usr/share/fonts/default/Type1/a010013l.pfb
        ...
    

    上面显示了一个示例,每种字体都有与上面相似的行。顺便说一句,运行此命令显示我们有数百种字体:

    $ convert -list font | grep Font | wc -l
    262
    
  2. 接下来,我们将\u2318使用我们拥有的每种字体来完成对字符进行编码的任务。这听起来很复杂,但是对于一些通过Bash精心设计的衬里来说却是微不足道的。

    $ for i in $(convert -list font | grep Font | awk '{print $2}'); \
        do convert -font $i -pointsize 36 label:⌘  ${i}.gif;done
    

    此代码段将使用for循环遍历每种字体,并运行convert命令的修改版本。

  3. 现在我们看一下结果。许多字体不能显示此特定字形,但可以显示几种,这似乎表明它不一定是ImageMagick中的错误,而是字体本身的限制。这是我可以显示此字形的字体的列表。

    • 迪亚·乌·桑斯·波尔德
    • DejaVu-Sans-Bold-Oblique
    • DejaVu-Sans-Book
    • DejaVu-Sans-浓缩大胆
    • DejaVu-Sans-Condensed-Bold-Oblique
    • DejaVu-Sans-浓缩
    • DejaVu-Sans-Condensed-Oblique
    • DejaVu-Sans-Mono-Bold
    • DejaVu-Sans-Mono-Bold-Oblique
    • DejaVu-Sans-Mono-Book
    • DejaVu-Sans-Mono-Oblique
    • DejaVu-Sans-Oblique
    • DejaVu-Serif-Bold
    • DejaVu-Serif-Bold-Italic
    • DejaVu-Serif-Book
    • DejaVu-Serif-浓缩粗体
    • DejaVu-Serif浓缩加粗斜体
    • DejaVu-Serif压缩
    • DejaVu-Serif-Condensed-Italic
    • DejaVu-Serif-Italic
    • FreeMono-Regular
    • FreeSerif-Regular
    • STIX-Math-Regular
    • STIX-常规
    • VL-哥特式

    我目睹了整个〜260个结果.gif文件,以确定哪些有效,哪些无效。这是一些可行的示例,以便您可以看到它们。

                                                ss1    ss2    ss3    ss4    ss5

参考文献


1
非常感谢您的分析!我放弃了……我用最终的解决方案更新了这个问题,该解决方案涉及使用SVG和inkscape将字体呈现到路径。
fommil 2014年

@fommil-似乎是字体问题。查看更新。
slm

在答案中包含最终命令(选择字体)会非常好
BorisDäppen2015年

对我来说,添加该选项-font DejaVu-Sans-Bold有助于为海盗头骨标记图像:perl -e 'binmode(STDOUT, ":utf8");print "Skull: \x{2620}";' |convert picture_orig.jpg -background none -fill red -pointsize 90 -font DejaVu-Sans-Bold label:@- -flatten picture_tagged.jpg感谢您的出色帮助!
BorisDäppen'15

谢谢@slm!您的回答有助于解决我的问题:unix.stackexchange.com/a/389474/9689
Grzegorz Wierzowiecki
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.