mattintosh note

どこかのエンジニアモドキの備忘録

2024-06-05: 現在ホビー関連の記事を 新しいブログ に移行しています(一部の国、ISP からは閲覧できません)

ImageMagick で艦これの改装画面を自動アレンジ

https://lh3.googleusercontent.com/-cXo1g_TKFC8/VmLnwv-6pgI/AAAAAAAANKQ/Jyh_YCQcdp0/d-h-Ic42/0000.png

久しぶりに ImageMagick を弄ってて反射効果の復習がしたかったので艦これ(艦隊これくしょん)の改装画面をキャプチャーしたものを convert 1回でアレンジするスクリプトを書いてみた。

必要なものは以下の画像6枚。艦これの画面は正確にクロップすれば800×480のはず。これは GIMP を使うと Autocrop Image が使えるので簡単。

https://lh3.googleusercontent.com/-IGYa9ua1AlE/VmLn2laCogI/AAAAAAAANKk/5hECAMkTf28/s400-Ic42/20151203_a01.png https://lh3.googleusercontent.com/-mzMsNiSIYxE/VmLn2gfhM1I/AAAAAAAANK0/9lSIvoQ7DRo/s400-Ic42/20151203_a02.png https://lh3.googleusercontent.com/-oktJyTlp6kg/VmLn2k_BHnI/AAAAAAAANKo/uOg06_TfgSA/s400-Ic42/20151203_a03.png https://lh3.googleusercontent.com/-b4kMCc5i9HY/VmLn3Fx_gII/AAAAAAAANK4/JFt9YeIJcl8/s400-Ic42/20151203_a04.png https://lh3.googleusercontent.com/-Y0FKkKVz0E4/VmLn3pGdK2I/AAAAAAAANK8/F152quJUNnA/s400-Ic42/20151203_a05.png https://lh3.googleusercontent.com/-VpOKFDRHmsU/VmLn3sX9gMI/AAAAAAAANLA/zTSOlEfVw5o/s400-Ic42/20151203_a06.png

フォントは Font Squirrel で手に入る Boycott を使用。

www.fontsquirrel.com

あとは以下のスクリプトの引数に画像ファイルをつけて実行するだけ。工程を何度かに分ければもう少しわかりやすいスクリプトになるけど1回で済ませたかったので若干カオスな状態に…。

#!/bin/ksh
set -e
for f
do
    test -f "${f}"
done
w=487 h=373 x=313 y=97 m=20 bg=gray5
convert \
    '(' \
        '(' "$1" -crop ${w}x${h}+${x}+${y}\! -alpha on '(' +clone -flip -size ${w}x$((${h}/2)) gradient:gray60-black -alpha off -compose CopyOpacity -composite ')' -append -background transparent -crop $((${w}+${m}))x$((${h}+(${h}/2)+${m}))-$((${m}/2))-$((${m}/2))\! -compose Over -flatten ')' \
        '(' "$2" -crop ${w}x${h}+${x}+${y}\! -alpha on '(' +clone -flip -size ${w}x$((${h}/2)) gradient:gray50-black -alpha off -compose CopyOpacity -composite ')' -append -background transparent -crop $((${w}+${m}))x$((${h}+(${h}/2)+${m}))-$((${m}/2))-$((${m}/2))\! -compose Over -flatten ')' \
        '(' "$3" -crop ${w}x${h}+${x}+${y}\! -alpha on '(' +clone -flip -size ${w}x$((${h}/2)) gradient:gray40-black -alpha off -compose CopyOpacity -composite ')' -append -background transparent -crop $((${w}+${m}))x$((${h}+(${h}/2)+${m}))-$((${m}/2))-$((${m}/2))\! -compose Over -flatten ')' \
        '(' "$4" -crop ${w}x${h}+${x}+${y}\! -alpha on '(' +clone -flip -size ${w}x$((${h}/2)) gradient:gray40-black -alpha off -compose CopyOpacity -composite ')' -append -background transparent -crop $((${w}+${m}))x$((${h}+(${h}/2)+${m}))-$((${m}/2))-$((${m}/2))\! -compose Over -flatten ')' \
        '(' "$5" -crop ${w}x${h}+${x}+${y}\! -alpha on '(' +clone -flip -size ${w}x$((${h}/2)) gradient:gray50-black -alpha off -compose CopyOpacity -composite ')' -append -background transparent -crop $((${w}+${m}))x$((${h}+(${h}/2)+${m}))-$((${m}/2))-$((${m}/2))\! -compose Over -flatten ')' \
        '(' "$6" -crop ${w}x${h}+${x}+${y}\! -alpha on '(' +clone -flip -size ${w}x$((${h}/2)) gradient:gray60-black -alpha off -compose CopyOpacity -composite ')' -append -background transparent -crop $((${w}+${m}))x$((${h}+(${h}/2)+${m}))-$((${m}/2))-$((${m}/2))\! -compose Over -flatten ')' \
        +append -background ${bg} -compose Over -flatten \
    ')' \
    -bordercolor ${bg} -border $((${m}/2))x$((${m}/2)) \
    -font Boycott-Regular \
    -fill white \
    -pointsize 96 -draw 'fill-opacity 1 gravity South text 0,60 "Combined Fleet Girls Collection"' \
    -pointsize 32 -draw 'fill-opacity 1 gravity South text 0,30 "Kantai Collection -KanColle-"' \
    /tmp/0000.png

原寸大だとこんな感じ。

横に長過ぎるからちょっとレイアウト変えたほうがいいかなぁ…。


左角の切り抜き加工を追加した。座標指定で切り抜かないで、マスクを作成して切り抜くことにした。透明化を維持したまま反射部分を作るのが複雑…。

#!/bin/ksh
set -e
for f
do
    test -f "${f}"
done
w=487 h=373 x=313 y=97 m=10 bg=gray4
convert \
    \( \
        \( \( "${1}" \( -size 800x480 xc:black -fill white -draw "path \"M $((${x}+20)),${y} L $((${x}+${w})),${y} L $((${x}+${w})),$((${y}+${h})) L $((${x}+20)),$((${y}+${h})) L ${x},$((${y}+${h}-20)) L ${x},$((${y}+20)) Z\"" -alpha off \) -compose CopyOpacity -composite -trim +repage \) \( +clone -flip \( -size ${w}x$((${h}/2)) xc:black -fill gradient:gray50-black -draw "path \"M 20,0 L ${w},0 L ${w},${h} L 20,${h} L 0,$((${h}-20)) L 0,20 Z\"" -alpha off \) -compose CopyOpacity -composite \) -append -background transparent -compose Over -flatten -trim +repage -bordercolor transparent -border ${m}x${m} \) \
        \( \( "${2}" \( -size 800x480 xc:black -fill white -draw "path \"M $((${x}+20)),${y} L $((${x}+${w})),${y} L $((${x}+${w})),$((${y}+${h})) L $((${x}+20)),$((${y}+${h})) L ${x},$((${y}+${h}-20)) L ${x},$((${y}+20)) Z\"" -alpha off \) -compose CopyOpacity -composite -trim +repage \) \( +clone -flip \( -size ${w}x$((${h}/2)) xc:black -fill gradient:gray40-black -draw "path \"M 20,0 L ${w},0 L ${w},${h} L 20,${h} L 0,$((${h}-20)) L 0,20 Z\"" -alpha off \) -compose CopyOpacity -composite \) -append -background transparent -compose Over -flatten -trim +repage -bordercolor transparent -border ${m}x${m} \) \
        \( \( "${3}" \( -size 800x480 xc:black -fill white -draw "path \"M $((${x}+20)),${y} L $((${x}+${w})),${y} L $((${x}+${w})),$((${y}+${h})) L $((${x}+20)),$((${y}+${h})) L ${x},$((${y}+${h}-20)) L ${x},$((${y}+20)) Z\"" -alpha off \) -compose CopyOpacity -composite -trim +repage \) \( +clone -flip \( -size ${w}x$((${h}/2)) xc:black -fill gradient:gray30-black -draw "path \"M 20,0 L ${w},0 L ${w},${h} L 20,${h} L 0,$((${h}-20)) L 0,20 Z\"" -alpha off \) -compose CopyOpacity -composite \) -append -background transparent -compose Over -flatten -trim +repage -bordercolor transparent -border ${m}x${m} \) \
        \( \( "${4}" \( -size 800x480 xc:black -fill white -draw "path \"M $((${x}+20)),${y} L $((${x}+${w})),${y} L $((${x}+${w})),$((${y}+${h})) L $((${x}+20)),$((${y}+${h})) L ${x},$((${y}+${h}-20)) L ${x},$((${y}+20)) Z\"" -alpha off \) -compose CopyOpacity -composite -trim +repage \) \( +clone -flip \( -size ${w}x$((${h}/2)) xc:black -fill gradient:gray30-black -draw "path \"M 20,0 L ${w},0 L ${w},${h} L 20,${h} L 0,$((${h}-20)) L 0,20 Z\"" -alpha off \) -compose CopyOpacity -composite \) -append -background transparent -compose Over -flatten -trim +repage -bordercolor transparent -border ${m}x${m} \) \
        \( \( "${5}" \( -size 800x480 xc:black -fill white -draw "path \"M $((${x}+20)),${y} L $((${x}+${w})),${y} L $((${x}+${w})),$((${y}+${h})) L $((${x}+20)),$((${y}+${h})) L ${x},$((${y}+${h}-20)) L ${x},$((${y}+20)) Z\"" -alpha off \) -compose CopyOpacity -composite -trim +repage \) \( +clone -flip \( -size ${w}x$((${h}/2)) xc:black -fill gradient:gray40-black -draw "path \"M 20,0 L ${w},0 L ${w},${h} L 20,${h} L 0,$((${h}-20)) L 0,20 Z\"" -alpha off \) -compose CopyOpacity -composite \) -append -background transparent -compose Over -flatten -trim +repage -bordercolor transparent -border ${m}x${m} \) \
        \( \( "${6}" \( -size 800x480 xc:black -fill white -draw "path \"M $((${x}+20)),${y} L $((${x}+${w})),${y} L $((${x}+${w})),$((${y}+${h})) L $((${x}+20)),$((${y}+${h})) L ${x},$((${y}+${h}-20)) L ${x},$((${y}+20)) Z\"" -alpha off \) -compose CopyOpacity -composite -trim +repage \) \( +clone -flip \( -size ${w}x$((${h}/2)) xc:black -fill gradient:gray50-black -draw "path \"M 20,0 L ${w},0 L ${w},${h} L 20,${h} L 0,$((${h}-20)) L 0,20 Z\"" -alpha off \) -compose CopyOpacity -composite \) -append -background transparent -compose Over -flatten -trim +repage -bordercolor transparent -border ${m}x${m} \) \
        +append -background ${bg} -compose Over -flatten \
    \) \
    -bordercolor ${bg} -border ${m}x${m} \
    -font Boycott-Regular \
    -fill white \
    -pointsize 96             -draw "fill-opacity 1 gravity South text 0,52 \"Combined Fleet Girls Collection\"" \
    -pointsize 32 -kerning 20 -draw "fill-opacity 1 gravity South text 0,20 \"Kantai Collection -KanColle-\"" \
    /tmp/0000.png

Twitter 用にサイズ調整したもの。