Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2722

電光掲示板シェル芸 その1

$
0
0

電光掲示板シェル芸とは?

営業中.png
電光掲示板シェル芸は、こういう電光掲示板風の文字列や画像を生成するシェル芸です。
シェル芸botでの実行例↓


電光掲示板シェル芸を、コマンドを実行しながら理解していきましょう。
前提として、imagemagickとtextimgがインストールされていること。
imagemagickだけでもできなくはないですが、textimgを使うと楽です。

シンプルな電光掲示板シェル芸

「あ」一文字を、黒地に赤の電光掲示板で表示してみましょう。

$ textimg あ|convert - -trim-compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/🍎🌑/'🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🍎🍎🍎🍎🍎🍎🌑🍎🌑🌑🍎🌑🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🌑🌑
🌑🌑🌑🌑🌑🌑🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🍎🍎🍎🍎🍎🌑🌑🌑🌑🌑
🌑🌑🌑🌑🍎🍎🍎🍎🍎🌑🌑🍎🍎🍎🍎🌑🌑🌑
🌑🌑🌑🍎🍎🍎🍎🌑🌑🌑🌑🍎🍎🌑🍎🍎🌑🌑
🌑🌑🍎🍎🌑🌑🍎🍎🌑🌑🌑🍎🌑🌑🌑🍎🍎🌑
🌑🍎🍎🌑🌑🌑🍎🍎🌑🌑🍎🍎🌑🌑🌑🍎🍎🌑
🌑🍎🍎🌑🌑🌑🍎🌑🌑🍎🍎🌑🌑🌑🌑🍎🍎🌑
🌑🍎🌑🌑🌑🌑🍎🍎🌑🍎🍎🌑🌑🌑🌑🌑🍎🌑
🌑🍎🍎🌑🌑🌑🍎🍎🍎🍎🌑🌑🌑🌑🌑🍎🍎🌑
🌑🍎🍎🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑🌑🍎🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑

動作原理を説明していきます。
まず textimg で「あ」の画像を作ります。

$ textimg あ -o/images/0.png

0.png

画像を imagemagick(convert) に渡して、周縁の余白をトリムして、無圧縮pbmフォーマットで出力します。

$ textimg あ|convert - -trim-compress none pbm:-
P1
18 19
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 
1 1 1 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 
1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 
1 1 1 0 0 0 0 1 1 1 1 0 0 1 0 0 1 1 
1 1 0 0 1 1 0 0 1 1 1 0 1 1 1 0 0 1 
1 0 0 1 1 1 0 0 1 1 0 0 1 1 1 0 0 1 
1 0 0 1 1 1 0 1 1 0 0 1 1 1 1 0 0 1 
1 0 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 
1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 0 0 1 
1 0 0 1 1 1 0 0 0 1 1 1 1 1 0 0 1 1 
1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 

1行目の P1 は無圧縮PBMであることを示しています。
2行目の 18 19 は、画像サイズが 18x19 であることを示してます。
あとは各ピクセルの色情報で、1が黒、0が白です。
色情報部分だけを抜き出して少し整形します。

$ textimg あ|convert - -trim-compress none pbm:-|tail -n +3|tr -d' '
111111111111111111
111111001111111111
111111001111111111
111000000101101011
110000000000000011
111111011111111111
111111001111111111
111111000000011111
111100000110000111
111000011110010011
110011001110111001
100111001100111001
100111011001111001
101111001001111101
100111000011111001
100111000111110011
110000001111000111
111111111110001111
111111111111111111

tailコマンドで最初の2行を除去、trコマンドでスペースを除去しました。
1と0を色表示用の文字で置換します。

$ textimg あ|convert - -trim-compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/🍎🌑/'🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🍎🍎🍎🍎🍎🍎🌑🍎🌑🌑🍎🌑🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🌑🌑
🌑🌑🌑🌑🌑🌑🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🍎🍎🍎🍎🍎🌑🌑🌑🌑🌑
🌑🌑🌑🌑🍎🍎🍎🍎🍎🌑🌑🍎🍎🍎🍎🌑🌑🌑
🌑🌑🌑🍎🍎🍎🍎🌑🌑🌑🌑🍎🍎🌑🍎🍎🌑🌑
🌑🌑🍎🍎🌑🌑🍎🍎🌑🌑🌑🍎🌑🌑🌑🍎🍎🌑
🌑🍎🍎🌑🌑🌑🍎🍎🌑🌑🍎🍎🌑🌑🌑🍎🍎🌑
🌑🍎🍎🌑🌑🌑🍎🌑🌑🍎🍎🌑🌑🌑🌑🍎🍎🌑
🌑🍎🌑🌑🌑🌑🍎🍎🌑🍎🍎🌑🌑🌑🌑🌑🍎🌑
🌑🍎🍎🌑🌑🌑🍎🍎🍎🍎🌑🌑🌑🌑🌑🍎🍎🌑
🌑🍎🍎🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑🌑🍎🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑

これで文字列としては完成です。
環境によっては文字幅が違って崩れているかもしれません。
twitterのシェル芸botでは文字数制限で途中で切れてしまいます。
これらの問題は、もう一度textimgに入れて画像にすることで回避できます。

$ textimg あ|convert - -trim-compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/🍎🌑/'|textimg -s

t.png

色の変更

上のサンプルでは背景色に🌑(新月)、文字色に🍎(りんご)を使用していますが、別の絵文字を使うことで色を変更することができます。

$ textimg あお|convert - -trim-compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/🍈⚪/'|textimg -s

t.png

$ textimg -z-F40💩|convert - -trim-compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/💩🌕/'|textimg -s-F10

t.png

thresholdで線をきれいに

線がガタガタしているのが気になるなら、pbm出力の前にthresholdをかけるときれいになる場合があります。

$ textimg あ|convert - -trim-threshold 50% -compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/🍎🌑/'|textimg -s

t.png

-threshold 50% とするとこのようになりました。
値を変えると、線の太さを変えることができます。
10%

$ textimg あ|convert - -trim-threshold 10% -compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/🍎🌑/'|textimg -s

t.png

70%

$ textimg あ|convert - -trim-threshold 70% -compress none pbm:-|tail -n +3|tr -d' '|sed 'y/01/🍎🌑/'|textimg -s

t.png

dtimgコマンド

dtimg は以上の操作を一発でやってくれるコマンドです。
https://github.com/jiro4989/scripts/blob/master/bin/dtimg

$ dtimg あ -b🌑 -g🍎 -R-F 20 -t 20
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🍎🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🌑🌑🌑🌑
🌑🌑🌑🌑🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🍎🌑🌑🌑
🌑🌑🌑🍎🍎🍎🍎🍎🍎🌑🌑🌑🍎🍎🍎🍎🍎🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🍎🌑🌑🍎🍎🍎🌑🍎🍎🍎🌑🌑
🌑🌑🍎🍎🍎🌑🍎🍎🍎🌑🌑🍎🍎🍎🌑🌑🍎🍎🌑🌑
🌑🌑🍎🍎🌑🌑🍎🍎🍎🌑🍎🍎🍎🌑🌑🌑🍎🍎🍎🌑
🌑🍎🍎🍎🌑🌑🍎🍎🍎🍎🍎🍎🌑🌑🌑🌑🍎🍎🌑🌑
🌑🌑🍎🍎🌑🌑🍎🍎🍎🍎🍎🍎🌑🌑🌑🍎🍎🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🍎🍎🍎🌑🌑🌑🍎🍎🍎🍎🌑🌑
🌑🌑🍎🍎🍎🍎🍎🍎🍎🌑🌑🌑🍎🍎🍎🍎🍎🌑🌑🌑
🌑🌑🌑🍎🍎🍎🍎🌑🌑🌑🌑🍎🍎🍎🍎🍎🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🍎🍎🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑
🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑

シェル芸botにも入っているので活用しましょう。
次回、駅の電光掲示板再現に続きます。


Viewing all articles
Browse latest Browse all 2722

Trending Articles