Skip to content

Instantly share code, notes, and snippets.

@crakaC
Last active November 28, 2017 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crakaC/9f8f33aa8179234b19148e602bc980f4 to your computer and use it in GitHub Desktop.
Save crakaC/9f8f33aa8179234b19148e602bc980f4 to your computer and use it in GitHub Desktop.
ImageMagickを弱小鯖用にビルド&インストールする

基本的にはInstall from Source @ ImageMagickの通りです。

ソースのダウンロード&展開

wget https://www.imagemagick.org/download/ImageMagick.tar.gz
tar xvzf ImageMagick.tar.gz

2017/11/27現在、7.0.7-11が降ってきます。

configure

cd ImageMagick7.0.7-11

./configure --disable-openmp --with-quantum-depth=8 --without-threads --disable-hdri
make

自分の鯖が弱い(1コア512MB)ので並列処理関係オプションは--disable-openmp--without-threadsとして切っています。 複数コアがある場合は普通に有効にしておいた方がいいと思います。

--with-quantum-depthは、1ピクセルの各色あたり何ビット使うか、というオプションです。 デフォルト値は16ですが、たいていの画像は8bitあればOKなので8にします。

--disable-hdriで、ハイダイナミックレンジ処理を無効にします。 これが有効だと、様々な計算が整数ではなく浮動小数点で行われるようになってしまうため、リソースを大幅に消費します。 HDRIフォーマットの画像がMastodonに飛んでくることはないはずなのでOKです。たぶん。

install

sudo make install
sudo ldconfig /usr/local/lib

確認

$ convert -version
Version: ImageMagick 7.0.7-11 Q8 x86_64 2017-11-27 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher
Delegates (built-in): jng jpeg png x xml zlib

ImageMagick 7.0.7-11 Q8になっていること、FeaturesにHDRIが含まれていないことが確認できます。

以上で完了です。

valgrindでImageMagickのメモリの挙動を確認するを参考に、 ビルドコンフィグに --disable-hdriを指定したものと、指定しなかったもので、サイズが6000x4000のjpeg画像をリサイズしたときのメモリ消費量を比較しました。

ついでにapt-getでインストールされるバージョン(ImageMagick 6.8.9-9 Q16)も合わせてチェックします。

テストするコマンドは、Mastodonにメディアをアップロードした時のものに合わせています。 また、jpegファイルをリサイズする時、-define jpeg:size=WxHでリサイズ後の大きさを指定すると高速&省メモリになるため、そちらも計測しました。

# 普通の
convert sample.jpg -auto-orient -quality 90 -resize 1280x1280 -strip out.jpg
# -define jpeg:sizeあり
convert -define jpeg:size=1280x1280 sample.jpg -auto-orient -quality 90 -resize 1280x1280 -strip out.jpg

結果

-define jpeg:sizeなし -define jpeg:sizeあり
--disable-hdriオプションなし 347.5MB 73.73MB
--disable-hdriオプションあり 87.22MB 18.77MB
ImageMagick 6.8.9-9 Q16 230.9MB 49.09MB

--disable-hdriオプションを付けることによって、メモリ消費量は1/4になりました。floatが4byte, unsigned_charが1byteなので、納得の行く結果です。 というか--disable-hdriオプションつけないとひどい…

おまけ

ImageMagick 6.8.9-9 Q16

--------------------------------------------------------------------------------
Command:            /usr/bin/convert sample.jpg -auto-orient -quality 90 -resize 1280x1280> -strip out.jpg
Massif arguments:   (none)
ms_print arguments: massif.out.22665
--------------------------------------------------------------------------------


    MB
230.9^                                                              ########
     |                 @::::::::::::::::::::::::::::::::::::::::::::#       :
     |                 @                                            #       :
     |                 @                                            #       :
     |                 @                                            #       :
     |@@@@@@@@@@@@@@@@@@                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
     |@                @                                            #       :
   0 +----------------------------------------------------------------------->Gi
     0                                                                   7.309

ImageMagick 6.8.9-9 Q16, -define jpeg:size

--------------------------------------------------------------------------------
Command:            /usr/bin/convert -define jpeg:size=1280x1280 sample.jpg -auto-orient -quality 90 -resize 1280x1280> -strip out.jpg
Massif arguments:   (none)
ms_print arguments: massif.out.22671
--------------------------------------------------------------------------------


    MB
49.09^                                                        ############
     |                                                        #           :
     |                                                        #           :
     |                                                        #           :
     |                              ::::::::::::::::::::::::::#           :
     |                              :                         #           :
     |                              :                         #           :
     |                              :                         #           :
     |                              :                         #           :
     |                              :                         #           :
     | :::::::::::::::::::::::::::::@                         #           :
     | :                            @                         #           :
     | :                            @                         #           :
     | :                            @                         #           :
     | :                            @                         #           :
     | :                            @                         #           :::
     | :                            @                         #           :  @
     | :                            @                         #           @  @
     | :                            @                         #           @  @
     | :                            @                         #           @  @
   0 +----------------------------------------------------------------------->Gi
     0                                                                   2.173

enable-hdri

--------------------------------------------------------------------------------
Command:            convert sample.jpg -auto-orient -quality 90 -resize 1280x1280 -strip out.jpg
Massif arguments:   (none)
ms_print arguments: q8_hdri_on.out
--------------------------------------------------------------------------------


    MB
347.5^                                                             :::::::::
     |             #::::::::::::::::::::::::::::::::::::::::::::::::        :
     |             #                                               :        :
     |             #                                               :        :
     |             #                                               :        :
     |@@@@@@@@@@@@@#                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
     |@            #                                               :        :
   0 +----------------------------------------------------------------------->Gi
     0                                                                   9.076

enable-hdri, -define jpeg:size=1280x1280

--------------------------------------------------------------------------------
Command:            convert -define jpeg:size=1280x1280 sample.jpg -auto-orient -quality 90 -resize 1280x1280 -strip out.jpg
Massif arguments:   (none)
ms_print arguments: q8_hdri_on_resize.out
--------------------------------------------------------------------------------


    MB
73.73^                                                     :::::::::::::::
     |                         #::::::::::::::::::::::::::::              :
     |                         #                           :              :
     |                         #                           :              :
     |                         #                           :              :
     |                         #                           :              :
     |                         #                           :              :
     |                         #                           :              :
     |                         #                           :              :
     |                         #                           :              :
     | @@@@@@@@@@@@@@@@@@@@@@@@#                           :              :
     | @                       #                           :              :
     | @                       #                           :              :
     | @                       #                           :              :
     | @                       #                           :              :
     | @                       #                           :              :
     | @                       #                           :              ::::
     | @                       #                           :              :  :
     | @                       #                           :              :  :
     | @                       #                           :              :  :
   0 +----------------------------------------------------------------------->Gi
     0                                                                   2.641

--disable-hdri

--------------------------------------------------------------------------------
Command:            convert sample.jpg -auto-orient -quality 90 -resize 1280x1280 -strip out.jpg
Massif arguments:   (none)
ms_print arguments: q8_no_hdri.out
--------------------------------------------------------------------------------


    MB
87.22^                                                             ::::::::::
     |             #::::::::::::::::::::::::::::::::::::::::::::::::         :
     |             #                                               :         :
     |             #                                               :         :
     |             #                                               :         :
     |@@@@@@@@@@@@@#                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
     |@            #                                               :         :
   0 +----------------------------------------------------------------------->Gi
     0                                                                   9.058

--disable-hdri, -define jpeg:size=1280x1280

--------------------------------------------------------------------------------
Command:            convert -define jpeg:size=1280x1280 sample.jpg -auto-orient -quality 90 -resize 1280x1280 -strip out.jpg
Massif arguments:   (none)
ms_print arguments: q8_no_hdri_resize_on.out
--------------------------------------------------------------------------------


    MB
18.77^                                                     ::::::::::::::::
     |                        #:::::::::::::::::::::::::::::               :
     |                        #                            :               :
     |                        #                            :               :
     |                        #                            :               :
     |                        #                            :               :
     |                        #                            :               :
     |                        #                            :               :
     |                        #                            :               :
     |                        #                            :               :
     | @@@@@@@@@@@@@@@@@@@@@@@#                            :               :
     | @                      #                            :               :
     | @                      #                            :               ::
     | @                      #                            :               :
     | @                      #                            :               :
     | @                      #                            :               : :
     | @                      #                            :               : :
     | @                      #                            :               : :
     | @                      #                            :               : :
     | @                      #                            :               : :
   0 +----------------------------------------------------------------------->Gi
     0                                                                   2.700
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment