hitode909の日記

趣味はマリンスポーツですの日記です

ImageMagickでliquid rescaleする

ImageMagickのリサイズまわりの挙動を調べてたら異常なアニメーションGIFが掲載されいているのを発見して,その瞬間に調べ物はどうでもよくなった.
領域が狭くなると,文字が帽子の上に周りこんでいる.
f:id:hitode909:20180509220055g:plain
http://www.imagemagick.org/Usage/resize/#liquid-rescale

Seam carving(liquird rescale)は画像のなかで自然に切り取れるところを探してリサイズする技術.物がないところを探して切り詰めるので物だけが残る.
Seam carving - Wikipedia

ImageMagickをliblqrつきでインストールすると使えるようになる.

brew install imagemagick --with-liblqr

オライリーの書影とか見るからに縮めやすくてめちゃくちゃ楽しいのでは?と思ったので練習.

Docker

Docker

convertに-liquid-rescaleを渡すとやってくれる.簡単.

convert original.jpg -liquid-rescale '100x80%!' 80.jpg

うまいこと余白が切り取られて縮んだ!めちゃくちゃたのしい
f:id:hitode909:20180509215858j:plain

アニメーションを作るスクリプトが配布されている
http://www.imagemagick.org/Usage/resize/animate_lqr

ので,書影を縦にびよんびよんして遊びたかったのでスクリプトを用意

#!/bin/sh
#
# Create a animation of of a rotating image.
# Some alpha composition is used to crop the result to the original size.
#
command="convert -delay 10 $1 -resize 50% -trim +repage -bordercolor blue"
command="$command \\( +clone -border 1x1 -fill lightsteelblue"
command="$command -colorize 100% \\) -gravity center"
for i in `seq 100 -2 20 ;`; do
command="$command \\( -clone 0 -liquid-rescale 100%x${i}\!"
command="$command -border 1x1 -clone 1 +swap -composite \\)"
done
# remove source images
command="$command -delete 0,1"
# duplicate frames to reverse
command="$command \\( -clone -2-1 \\)"
# write out animation
command="$command -layers Optimize -loop 0 animated_lqr.gif"
eval $command
chmod 644 animated_lqr.gif
gist.github.com

めちゃくちゃ楽しくないですか? めちゃくちゃ楽しいのでみなさんもやりましょう.
f:id:hitode909:20180509215520g:plain