LoginSignup
0
0

More than 1 year has passed since last update.

RaspberryPi の convert コマンドで 日本語

Posted at

RaspberryPi を使って、テレカンファレンスのリアクションシステムを作っています。
https://github.com/nanbuwks/laughslap

リモートで拍手や歓声などの音を出すシステムですが、これのサブ機能応用として、RaspberryPi のフレームバッファにいろんな画像を表示し、PR画像やプレゼンタイマーなどに活用することができます。

fbi コマンドで png 画像を表示しています。それに伴い、RaspberryPi で日本語テキストから png 画像を生成する方法を調べてみました。

環境

Raspberry Pi OS Lite 2020-12-02版

convert コマンドの確認

まずは、英語テキストから画像を生成する方法の確認をしてみます。

$ convert -size 480x120 -background "rgb(#A0FFFF)" -pointsize 28 label:"Let's online reaction"  txtpanel.png

以下の画像が得られます。

image.png

フォントを指定して描画してみます。


$ convert -size 480x120 -background "rgb(#A0FFFF)" -pointsize 28 -font Nimbus-Mono-L-Bold label:"Let's online reaction"  txtpanel.png 

image.png

うまくいきましたね。日本語フォントを指定すれば日本語が出るかな?

日本語で convert

まずはフォント指定なしで試してみます。


$ convert -size 480x120 -background "rgb(#A0FFFF)" -pointsize 28 label:"online 拍手!"  txtpanel.png 

image.png

日本語の部分が空白になっています。

「ImageMagickで全世界のテキストを文字化けせずに表示する方法」
https://qiita.com/engineer/items/ab390a08ff53a664e15b

のように unifont を指定してみます。

convert -size 480x120 -background "rgb(#A0FFFF)" -pointsize 28 -font $(fc-match --format=%{file} unifont) label:"online 拍手!"  txtpanel.png 

としてみます。
image.png

しかしながら、コレでもダメです。ここで unifont として選択されたフォントはどれのことなのでしょうか?


$ fc-match --format=%{file} unifont
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc

と出ます。

/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf

を見てみると、


$ ls -alh /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
-rw-r--r-- 1 root root 740K Jul 30  2016 /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf

ファイルサイズから見て明らかにCJKあたりの情報は入って無いですね

日本語フォントをインストール


$ sudo apt-cache search CJK

として、


cjk-latex - installs all LaTeX CJK packages
cmap-adobe-cns1 - CMaps for Adobe-CNS1
cmap-adobe-gb1 - CMaps for Adobe-GB1
cmap-adobe-japan1 - CMaps for Adobe-Japan1
cmap-adobe-japan2 - CMaps for Adobe-Japan2
cmap-adobe-korea1 - CMaps for Adobe-Korea1
debiandoc-sgml - DebianDoc SGML DTD and formatting tools
fonts-arphic-bkai00mp - "AR PL KaitiM Big5" Chinese TrueType font by Arphic Technology
fonts-arphic-bsmi00lp - "AR PL Mingti2L Big5" Chinese TrueType font by Arphic Technology
fonts-arphic-gbsn00lp - "AR PL SungtiL GB" Chinese TrueType font by Arphic Technology
fonts-arphic-gkai00mp - "AR PL KaitiM GB" Chinese TrueType font by Arphic Technology
fonts-arphic-ukai - "AR PL UKai" Chinese Unicode TrueType font collection Kaiti style
fonts-arphic-uming - "AR PL UMing" Chinese Unicode TrueType font collection Mingti style
fonts-babelstone-han - BabelStone Fonts for Han
fonts-baekmuk - Baekmuk series Korean fonts
fonts-ipafont-gothic - Japanese OpenType font set, IPA Gothic and IPA P Gothic Fonts
fonts-ipafont-mincho - Japanese OpenType font set, IPA Mincho and IPA P Mincho Fonts
fonts-noto-cjk - "No Tofu" font families with large Unicode coverage (CJK regular and bold)
fonts-noto-cjk-extra - "No Tofu" font families with large Unicode coverage (CJK all weight)
fonts-wqy-microhei - Sans-serif style CJK font derived from Droid
fonts-wqy-zenhei - "WenQuanYi Zen Hei" A Hei-Ti Style (sans-serif) Chinese font
gs-cjk-resource - Resource files for gs-cjk, ghostscript CJK-TrueType extension
im-config - Input method configuration framework
im-switch - Input method switch framework
latex-cjk-all - installs all LaTeX CJK packages
latex-cjk-chinese - Chinese module of LaTeX CJK
latex-cjk-chinese-arphic-bkai00mp - AR PL KaitiM Big5 fonts for CJK
latex-cjk-chinese-arphic-bsmi00lp - AR PL Mingti2L Big5 fonts for CJK
.
.
.

と出てきました。このうち以下の2つをインストールしてみました。

  • fonts-noto-cjk-extra
  • fonts-ipafont-gothic
$ sudo apt install fonts-noto-cjk-extra fonts-ipafont-gothic

としてインストール。

インストールした日本語フォントを指定して描画する



$ fc-match --sort :lang=ja
fonts-japanese-gothic.ttf: "IPAGothic" "Regular"
DejaVuSans.ttf: "DejaVu Sans" "Book"
DejaVuSans-Bold.ttf: "DejaVu Sans" "Bold"
n019003l.pfb: "Nimbus Sans L" "Regular"
NotoSansCJK-Regular.ttc: "Noto Sans CJK JP" "Regular"
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"
DejaVuSerif.ttf: "DejaVu Serif" "Book"
NotoSerifCJK-Regular.ttc: "Noto Serif CJK JP" "Regular"
DroidSansFallbackFull.ttf: "Droid Sans Fallback" "Regular"
s050000l.pfb: "Standard Symbols L" "Regular"
DejaVuSansMono-Bold.ttf: "DejaVu Sans Mono" "Bold"
DejaVuSerif-Bold.ttf: "DejaVu Serif" "Bold"

となりましたね。fonts-noto-cjk-extra を入れたので、フォントウェイトで Black なども使えるはずですが、どうしたらいいかな? よくわからなかったのでフォントファイルを直接指定してみます。


$ find /usr/share/fonts/ -name *CJK*
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc
/usr/share/fonts/opentype/noto/NotoSansCJK-Medium.ttc
/usr/share/fonts/opentype/noto/NotoSansCJK-Thin.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-Black.ttc
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-ExtraLight.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-Light.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-Medium.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-SemiBold.ttc
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc
/usr/share/fonts/opentype/noto/NotoSansCJK-DemiLight.ttc
/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc

としてフォントファイルの場所を取得して、

$ convert -size 480x120 -background "rgb(#A0FFFF)" -pointsize 28 -font /usr/share/fonts/opentype/noto/NotoSerifCJK-Black.ttc label:"online 拍手!"  txtpanel.png 

とすると

image.png

うまくできました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0